c++ - Printing data from a text file using Cpp in Linux -
i want print out set of integers, written down 1 on each line in text file. since using linux, cannot seem use convenient functions getch() , getline()
please read code below , tell me need change see integers in text file
#include <iostream> #include <fstream> using namespace std; int main() { fstream fp; fp.open("/home/documents/c/codeblocks/test1.txt", ios::in); int c; if(!fp) { cout<<"cannot open file\n"; return 1; } while(fp) { fp.get(c); cout << c; } //while ((c = getline(fp) != eof)) // printf("%d\n", c); }
really nice way read things file using streams. using them can read numbers seperated whitespaces (e.g. newline, spaces, etc.) using >>
operator. please read first answer in following article, suits issue:
read numeric data text file in c++
what is
int c; while (fp >> c) { cout << c << " "; }
also, don't have split declaration , definition of fstream fp;
variable in case. put
fstream myfile("/home/documents/c/codeblocks/test1.txt", ios::in);
Comments
Post a Comment