How To Grab Data From A File And Fill It Into An Array C++
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Ok here is the code that I desire to manipulate:
#include<iostream> #include<fstream> int readfile() { ifstream myfile; string str_line; int flag=0; // used to check if the file already open or not if ( flag ==0) // initially file is not open and so open up it here { myfile.open(file.txt); flag=ane; // set the flag that file is opened now } if (myfile.is_open()) { cout<<"File Opening Success"; flag=1; getline(myfile, str_line); // get 1 line at a time. After getting this one line, I desire to laissez passer this 1 line data to another process and // get dorsum to here once more for the next line to process. without opening the file over again cout<<str_line; if(myfile.eof()) // if eof reached shut the file. { myfile.close(); flag = -1; exit; } } else { TRACE("File Opening NOT Success"); flag = -1; exit; } return 0; }
Trouble with this lawmaking is, as soon equally I loop back to this function(readfiel), it not proceed opening the file. and the condition check is file open up() fail.
In short my problem is, I want to porcess the file line by line and each time I read a line, send information technology to another procedure to and later processing the line come dorsum to this function and read the next line.
Thanks in accelerate for your assistance and understanding.
Ancient Dragon five,243 Achieved Level 70 Squad Colleague Featured Affiche
You can't call that part in a loop to read the file line-by-line because the file pointer is destroyed as before long as the function returns to it's caller. Yous need to call that function only once, so have that role read each line in a loop. Yous volition almost likely take to reorganize the way the rest of your program works. You didn't post information technology and so I don't know what it does.
The check for file open on line x doesn't work they way you desire it to because the file is automatically closed when the function exits. The flag on line 8 therefore becomes useless. What that function should do is something like this:
open up the file kickoff of loop read a line exit this loop if end-of-file reached procedure the line get dorsum to start of loop end of loop
It can be hands coded like this:
ifstream infile("myfile.txt"); std::string line; if( infile.is_open() ) { while( getline(infile,line) ) { // practise something hither } }
Aboriginal Dragon v,243 Achieved Level 70 Team Colleague Featured Poster
If yous don't want to do information technology all within that office and then yous have to laissez passer a pointer to the office.
#include<iostream> #include<fstream> ifstream& readfile(ifstream& myfile) { if( getline(myfile, str_line) ) { cout<<str_line; } return myfile; } int main() { ifstream myfile("myfile.txt"); if( myfile.is_open() ) { while( readfile(myfile) ) { // practise something } } }
But why go to all the problem to call readline() when you could telephone call getline() easier direct within the while loop? No 1 in his correct mind would attempt to lawmaking it the way yous described -- too complicated and simply too damned sloooow.
How To Grab Data From A File And Fill It Into An Array C++,
Source: https://www.daniweb.com/programming/software-development/threads/470869/read-a-file-in-c-into-an-array
Posted by: crowprieture.blogspot.com
0 Response to "How To Grab Data From A File And Fill It Into An Array C++"
Post a Comment