(1)以下程序是建立一个名为myfile的文件,把从键盘输入的字符存入该文件,当键盘上输入结束时关闭该文件。为实现上述功能,请完成程序。 #include "stdio.h" int main() { FILE*fp ; char ch; fp=fopen("myfile.txt","w"); while((ch = getchar( )) !='\n' ) ; ; return 0; }
时间: 2024-02-15 18:09:31 浏览: 50
以下是完成程序的代码:
```c
#include <stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen("myfile.txt", "w");
if (fp == NULL) {
printf("Failed to create file!\n");
return 1;
}
printf("Please input the characters (end with Enter):\n");
while ((ch = getchar()) != '\n') {
fputc(ch, fp);
}
fclose(fp);
printf("File saved successfully!\n");
return 0;
}
```
程序的主要思路是先打开一个名为"myfile.txt"的文件,使用`fopen()`函数实现。然后通过`getchar()`函数从键盘上读取输入的字符,并使用`fputc()`函数将字符逐个写入文件中。当输入结束时,使用`fclose()`函数关闭文件,并输出提示信息表示文件保存成功。
相关问题
在程序中创建一个文本文件myfile.txt,写入从键盘输入的一串字符
好的,您可以使用以下代码来创建一个文本文件myfile.txt,并将从键盘输入的一串字符写入该文件中:
```python
# 打开文件,如果文件不存在则会创建
file = open("myfile.txt", "w")
# 从键盘读取输入并写入文件中
text = input("请输入一串字符:")
file.write(text)
# 关闭文件
file.close()
```
请注意,如果您在程序运行时多次运行此代码,它将覆盖myfile.txt文件中的任何现有内容。如果您想将新文本附加到文件末尾而不是覆盖现有内容,请将打开文件的模式从"w"更改为"a"。
从键盘输入一串字符,以#号结束,输入过程中将输入字串写入到test文件夹myfile.txt文件中,然后再从该文件读取字符串显示到屏幕上。
以下是Java代码实现:
```java
import java.io.*;
public class Main {
public static void main(String[] args) {
try {
// 从键盘读取输入
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
FileWriter writer = new FileWriter("test/myfile.txt");
System.out.println("请输入一段字符串,以#号结束:");
String line;
while ((line = reader.readLine()) != null) {
// 将输入写入文件中
writer.write(line);
if (line.endsWith("#")) {
break;
}
}
reader.close();
writer.close();
// 从文件中读取字符串并输出到屏幕
FileReader fileReader = new FileReader("test/myfile.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder stringBuilder = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
bufferedReader.close();
fileReader.close();
System.out.println("读取到的字符串为:" + stringBuilder.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
首先,我们通过`BufferedReader`从键盘读取输入,并使用`FileWriter`将输入写入到`test/myfile.txt`文件中。在输入过程中,我们判断输入的字符串是否以`#`号结尾,如果是,则跳出循环。
接着,我们通过`FileReader`和`BufferedReader`从文件中读取字符串,并使用`StringBuilder`拼接成完整的字符串。最后,我们将读取到的字符串输出到屏幕上。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)