C语言读取文件的一行:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 200
int main(int argc, char* argv[])
{
FILE* fp;
char buffer[MAX_LINE];
fp = fopen("test.txt", "r");
if (fp == NULL)
{
perror("File open");
exit(1);
}
while (fgets(buffer, MAX_LINE, fp) != NULL)
{
fputs(buffer, stdout);
}
return 0;
}
程序输出:
C++语言读取文件的一行:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(int argc, char** argv)
{
ifstream ifs("test.txt");
string str;
while (getline(ifs, str))
{
cout << str << endl;
}
return 0;
}
程序输出:
总结:C语言使用fgets()读取文件的一行内容,C++使用getline()读取文件的一行内容。
C++的getline比较形象的说明了可以读取一样内容。
原文链接: https://www.cnblogs.com/Robotke1/archive/2013/05/15/3079648.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/88557
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!