逐行读取C和C ++?

我想从C或C ++中的文件中逐行读取,并且当我假设某个行的固定大小时,我知道如何做到这一点,但是有一种简单的方法可以以某种方式计算或获得行所需的确切大小或文件中的所有行? (如果任何人都可以这样做的话,直到换行一字一句也对我有好处。)

如果您使用流式读卡器,则所有这些都将对您隐藏。 见getline 。 以下示例基于此处的代码。

 // getline with strings #include  #include  #include  using namespace std; int main () { string str; ifstream ifs("data.txt"); getline (ifs,str); cout << "first line of the file is " << str << ".\n"; } 

在C中,如果您有POSIX 2008库(例如,更新版本的Linux),则可以使用POSIX getline()函数。 如果你的库中没有这个function,你可以很容易地实现它,这可能比发明你自己的界面来完成这项工作要好。

在C ++中,您可以使用std::getline()

尽管这两个函数具有相同的基本名称,但调用约定和语义却完全不同(因为语言C和C ++完全不同) – 当然,它们都从文件流中读取一行数据。

没有一种简单的方法可以判断文件中最长行的大小 – 除了通过读取整个文件来查找,这有点浪费。

我会使用IFStream并使用getline从文件中读取。

http://www.cplusplus.com/doc/tutorial/files/

 int main () { string line; ifstream myfile ("example.txt"); if (myfile.is_open()) { while ( myfile.good() ) { getline (myfile,line); cout << line << endl; } myfile.close(); } else cout << "Unable to open file"; return 0; } 

在读入之前,您无法获得行的长度。但是,您可以反复读入缓冲区,直到到达行尾。

对于c中的编程,请尝试使用fgets读取一行代码。 如果遇到换行符,它将读取n个字符或停止。 您可以读取大小为n的小缓冲区,直到字符串中的最后一个字符为换行符。

有关更多信息,请参阅上面的链接。

以下是如何使用小缓冲区读取整行文件的示例:

 #include  #include  int main() { FILE * pFile; const int n = 5; char mystring [n]; int lineLength = 0; pFile = fopen ("myfile.txt" , "r"); if (pFile == NULL) { perror ("Error opening file"); } else { do { fgets (mystring , n , pFile); puts (mystring); lineLength += strlen(mystring); } while(mystring[strlen ( mystring)-1] != '\n' && !feof(pFile)); fclose (pFile); } printf("Line Length: %d\n", lineLength); return 0; } 

在C ++中,您可以使用std::getline函数,它接受一个流并读取第一个'\n'字符。 在C中,我只使用fgets并继续重新分配缓冲区,直到最后一个字符为'\n' ,然后我们知道我们已经读完了整行。

C ++:

 std::ifstream file("myfile.txt"); std::string line; std::getline(file, line); std::cout << line; 

C:

 // I didn't test this code I just made it off the top of my head. FILE* file = fopen("myfile.txt", "r"); size_t cap = 256; size_t len = 0; char* line = malloc(cap); for (;;) { fgets(&line[len], cap - len, file); len = strlen(line); if (line[len-1] != '\n' && !feof(file)) { cap <<= 1; line = realloc(line, cap); } else { break; } } printf("%s", line); 

getline只是POSIX,这里是ANSI(不需要最大行数信息!):

 const char* getline(FILE *f,char **r) { char t[100]; if( feof(f) ) return 0; **r=0; while( fgets(t,100,f) ) { char *p=strchr(t,'\n'); if( p ) { *p=0; if( (p=strchr(t,'\r')) ) *p=0; *r=realloc(*r,strlen(*r)+1+strlen(t)); strcat(*r,t); return *r; } else { if( (p=strchr(t,'\r')) ) *p=0; *r=realloc(*r,strlen(*r)+1+strlen(t)); strcat(*r,t); } } return feof(f)?(**r?*r:0):*r; } 

现在你的主要是简单和简短的:

  char *line,*buffer = malloc(100); FILE *f=fopen("yourfile.txt","rb"); if( !f ) return; setvbuf(f,0,_IOLBF,4096); while( (line=getline(f,&buffer)) ) puts(line); fclose(f); free(buffer); 

它适用于Windows和Unix-textfiles的Windows,适用于Unix和Unix文本文件

下面是使用std算法和迭代器读取行的C ++方法:

 #include  #include  #include  #include  struct getline : public std::iterator { std::istream* in; std::string line; getline(std::istream& in) : in(&in) { ++*this; } getline() : in(0) { } getline& operator++() { if(in && !std::getline(*in, line)) in = 0; } std::string operator*() const { return line; } bool operator!=(const getline& rhs) const { return !in != !rhs.in; } }; int main() { std::vector v; std::copy(getline(std::cin), getline(), std::back_inserter(v)); }