Write a function that opens a file (input: filename), and prints the file line by line
时间: 2024-05-24 21:11:49 浏览: 7
def print_file(filename):
with open(filename, "r") as f:
for line in f:
print(line.strip())
print_file("example.txt") # replace example.txt with your filename
相关问题
Write a C++ program that implements flipping the contents of each line of a string in a text file and outputs the flipped result to another text file. Both the input file name and output file name are specified by the user.
Sure, here's a C++ program that implements flipping the contents of each line of a string in a text file and outputs the flipped result to another text file.
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string inputFileName, outputFileName;
cout << "Enter input file name: ";
cin >> inputFileName;
cout << "Enter output file name: ";
cin >> outputFileName;
ifstream inputFile(inputFileName);
if (!inputFile.is_open())
{
cout << "Failed to open input file!" << endl;
return 1;
}
ofstream outputFile(outputFileName);
if (!outputFile.is_open())
{
cout << "Failed to open output file!" << endl;
return 1;
}
string line;
while (getline(inputFile, line))
{
reverse(line.begin(), line.end());
outputFile << line << endl;
}
inputFile.close();
outputFile.close();
cout << "File has been flipped and saved successfully!" << endl;
return 0;
}
```
The program first prompts the user to enter the input file name and output file name. It then opens the input file and output file using `ifstream` and `ofstream`, respectively. If either file cannot be opened, the program reports an error and exits.
The program then reads each line from the input file using `getline`, reverses the line using `reverse`, and writes the reversed line to the output file using `outputFile << line << endl`. Finally, the program closes both files and reports success.
Note that this program assumes that the input file contains one line of text per line, and that each line ends with a newline character. If the input file does not conform to this format, the program may not work correctly.
Make a file that prompts the use for a filename, open the mbox-short.txt file and count the number of records.
Here's a Python code that prompts the user for a filename, opens the mbox-short.txt file, and counts the number of records in it:
```
filename = input("Enter the filename: ")
try:
fhandle = open(filename)
except:
print("File not found:", filename)
exit()
count = 0
for line in fhandle:
if line.startswith("From "):
count += 1
print("There are", count, "records in the file.")
```
Explanation:
1. The `input()` function prompts the user to enter a filename, which is stored in the `filename` variable.
2. The `try-except` block checks if the file exists or not. If the file is not found, it prints an error message and exits the program using the `exit()` function.
3. The `count` variable is initialized to 0.
4. A `for` loop is used to iterate over each line in the file.
5. The `startswith()` method is used to check if the line starts with the string "From ". This is because each record in the mbox-short.txt file starts with this string.
6. If a line starts with "From ", the `count` variable is incremented by 1.
7. After the loop completes, the program prints the number of records counted using the `print()` function.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)