so i'm stuck trying figured bug on program preventing me displaying text of program..
#include <iostream> #include <iomanip> #include <fstream> #include <sstream> #include <string> #include <stdio.h> using namespace std; int main () { ifstream infile; ofstream offile; char text[1024]; cout <<"please enter name of file: \n"; cin >> text; infile.open(text); string scores; // lines... getline(infile, scores, '\0'); // i'm using... cout << scores << endl; // display file... string name1; int name2; string name3; int name4; infile >> name1; infile >> name2; infile >> name3; infile >> name4; cout << "these 2 individual age add are" << name2 + name4 <<endl; // 23 + 27 //the result bunch of numbers... return 0; } is there way cleaner or simple method can used display file ?
all method in internet difficult understand or keep track due file open in loop..
i want program type name of file , displays file file contain following...
jack 23 smith 27 also need obtain data file i'm using above code obtain information file...
loop best thing can do. if know format this
#include <iostream> #include <fstream> using namespace std; int printparsedfile(string filename) { // declaration of function reads file passed argument fstream f; // file stream f.open(filename.c_str(), ios_base::in); // open file reading if (f.good()) { // check if file can read string tmp; // temp variable use getting chunked data while(!f.eof()) { // read data until end of file reached f >> tmp; // first chunk of data cout << tmp << "\t"; // , print console f >> tmp; // chunk cout << tmp << endl; // , print } else { return -1; // failed open file } return 0; // file opened , read } you can call function example in main() function read , display file passed argument
int main(int argc, char** argv) { string file; cout << "enter name of file read from: " cin >> file; printparsedfile(file); return 0; }
Comments
Post a Comment