C++--读取txt文件中的多行数据
时间: 2023-10-09 09:16:38 浏览: 141
Sure, I'd be happy to help you with that! Here's an example of how you can read multiple lines from a text file in the C programming language:
```c
#include <stdio.h>
int main() {
FILE *fp;
char line[255];
fp = fopen("filename.txt", "r");
while (fgets(line, sizeof(line), fp)) {
printf("%s", line);
}
fclose(fp);
return 0;
}
```
In this example, we first open a file called "filename.txt" for reading using the `fopen` function. We then use a `while` loop to read one line at a time using `fgets`, which reads up to `sizeof(line)` characters from the file at a time. Finally, we print each line to the console using `printf`. Don't forget to close the file using `fclose` when you're done!
阅读全文