天天看点

C++从文本文件中读一行数据

C/C++ 的IO流当遇到space和enter符号的时候会自动结束读入。而一行文本往往多多少少会有空格。 以下给出了一个实例。

#include <iostream> #include <fstream> #include<string> using namespace std ; int CheckParm ( int argc ) {   if( argc <= 1 )   {     cout << "Usage: SQLAdd srcfile.txt destfile prefix stufix" << endl ;     return 0 ;   }   return 1 ; } int main ( int argc , char * argv []) {   ifstream in ;   ofstream out ;   char buf [ 1024 ] = { 0 };   if(! CheckParm ( argc ))     return 0 ;   in . open ( argv [ 1 ], ios_base :: in );   out . open ( argv [ 2 ], ios_base :: out );   while( in . getline ( buf , sizeof ( buf ))) {     string str = buf ;     cout << str << endl ;     str = "strSQL &= /"" + str + " /"& vbcrlf" ;     out << str << endl ;     cout << str << endl ;   }   in . close ();   out . close ();   return 0 ; } 原文:http://tb.blog.csdn.net/TrackBack.aspx?PostId=630347