编写程序: 让用指定写入文本的文件名字和位置,将以追加方式打开该文件。 用户从键盘上输入多行文字,将每行文字内容写入文件中,如果用户 输入end 则结束输入。 打开刚才所写入的文件,读取每一行文字输出到屏幕上。
时间: 2024-03-13 07:43:03 浏览: 18
以下是 Python 代码实现:
```python
filename = input("请输入文件名和位置:")
with open(filename, 'a') as f:
while True:
line = input("请输入要写入文件的内容,输入 end 结束:")
if line == "end":
break
f.write(line + "\n")
with open(filename, 'r') as f:
for line in f:
print(line.strip())
```
解释:
首先让用户输入文件名和位置,并使用 `with open(filename, 'a') as f:` 语句以追加方式打开文件。然后进入循环,让用户输入要写入文件中的内容,如果输入了 "end" 则退出循环。在循环中,使用 `f.write(line + "\n")` 将每一行输入写入文件中。
接下来使用 `with open(filename, 'r') as f:` 语句以读取方式打开文件,然后使用 `for line in f:` 循环读取每一行文字,并使用 `print(line.strip())` 将其输出到屏幕上。其中 `line.strip()` 用于去掉每一行文字末尾的换行符。
相关问题
java编写程序: 让用指定写入文本的文件名字和位置,将以追加方式打开该文件。 用户从键盘上输入多行文字,将每行文字内容写入文件中,如果用户 输入end 则结束输入。 打开刚才所写入的文件,读取每一行文字输出到屏幕上。
以下是 Java 代码实现:
```java
import java.io.*;
public class FileIO {
public static void main(String[] args) {
String filename = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = null;
try {
System.out.print("请输入文件名和位置:");
filename = reader.readLine();
writer = new BufferedWriter(new FileWriter(filename, true));
while (true) {
System.out.print("请输入要写入文件的内容,输入 end 结束:");
String line = reader.readLine();
if (line.equals("end")) {
break;
}
writer.write(line);
writer.newLine();
}
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
BufferedReader fileReader = null;
try {
fileReader = new BufferedReader(new FileReader(filename));
String line = null;
while ((line = fileReader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileReader != null) {
fileReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
解释:
首先定义一个 `BufferedReader` 对象 `reader`,用于从键盘上读取用户的输入。然后让用户输入文件名和位置,并使用 `BufferedWriter` 对象 `writer` 以追加方式打开该文件。进入循环,让用户输入要写入文件中的内容,如果输入了 "end" 则退出循环。在循环中,使用 `writer.write(line)` 将每一行输入写入文件中,并使用 `writer.newLine()` 添加一个换行符。
接下来使用 `BufferedReader` 对象 `fileReader` 以读取方式打开文件,然后使用 `while ((line = fileReader.readLine()) != null)` 循环读取每一行文字,并使用 `System.out.println(line)` 将其输出到屏幕上。其中 `line` 表示读取到的每一行文字。最后需要在程序结束前关闭文件流。
使用c语言编写程序:从键盘输入若干实数,将实数写入文件,以特殊数值-1 结束
好的,以下是使用 C 语言编写的程序,可以从键盘输入若干实数,将实数写入文件,并以特殊数值-1结束:
```c
#include <stdio.h>
int main() {
float num;
FILE *file;
file = fopen("numbers.txt", "w");
if (file == NULL) {
printf("无法打开文件!");
return 0;
}
while (1) {
printf("请输入实数(输入-1结束):");
scanf("%f", &num);
if (num == -1) {
fprintf(file, "%f", num);
break;
}
fprintf(file, "%f\n", num);
}
fclose(file);
return 0;
}
```
这段代码会先要求您输入实数,然后将其写入文件中,直到输入-1为止。如果无法打开文件,则会提示无法打开文件并退出程序。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)