Reading a text file

Jun 20, 2014 at 5:27pm
Hello everyone, I am new to this forum and I challenge getting my code to read text file located on the desktop. When I ran this code, a fatal error message shows like: No such file or directory

#include <iostream>
#include <string>
#include <fstream>
#include <ifstream>

using namespace std;

int main()
{
ifstream myfile;
myfile.open ("C:/Users/UGONNA/Desktop/Book1.txt", ios::in);

return 0;
}


Last edited on Jun 20, 2014 at 5:28pm
Jun 20, 2014 at 5:34pm
Remove #include <ifstream> line.
Jun 21, 2014 at 6:11pm
Thank you MiiNiPaa. The error message was cancelled, but I still cannot read the text file from my computer. My aim is to read a file from the computer and then create another file in which I will write the read texts. The code I wrote for the task is:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;


int main()
{
ifstream infile;
ofstream outfile;
infile.open ("C:/Users/UGONNA/Desktop/Book1.txt", ios::in);
outfile.open ("C:/Users/UGONNA/Desktop/Book2.txt", ios::out);

char scores [7];
while (!infile.eof ())
{
infile >> scores;
outfile << scores;
}

return 0;
}

Last edited on Jun 21, 2014 at 6:50pm
Jun 21, 2014 at 6:18pm
Check if file is open after you try to open it. If it is not:
1) Is this a correct path?
2) Do your application has access to file?
Jun 21, 2014 at 6:24pm
Your code as it stands right now just opens the file. By read if you mean to print the contents of the file to the console you can do something like this.
#include <iostream>
#include <fstream>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main () 
{
  std::ifstream ifs;

  ifs.open ("example.txt", std::ifstream::in);

  char c = ifs.get();

  while (ifs.good()) 
  {
    std::cout << c;
    c = ifs.get();
  }

  ifs.close();

  return 0;
}

or if you've tried to extract information from your file in some other manner and that code fails post it here.

http://www.cplusplus.com/doc/tutorial/files/
could be usefull
Last edited on Jun 21, 2014 at 6:25pm
Jun 24, 2014 at 9:46am
Thank a lot MiiNiPaa. The path is correct and the code accesses the file, but it cannot read and write on another file. I have tried another yet:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;


int main()
{

ifstream infile;
ofstream outfile;
infile.open ("C:/Users/UGONNA/Desktop/Book1.txt", ios::in);
outfile.open ("C:/Users/UGONNA/Desktop/Book8.txt", ios::out);

string scores;
int $numBERS [20];
// while (infile.eof ())

getline (infile, scores);
outfile << scores;
getline (infile, $numBERS [20]);
outfile << $numBERS;


return 0;
}
Jun 24, 2014 at 9:53am
and the code accesses the file
So infile.is_open() returns true?
Jun 24, 2014 at 6:53pm
Use This ...It will Work Fine


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std ;

int main()
{
	
	fstream file1("C:/Users/MR_Salman/Desktop/exper.txt");
	fstream file2("C:/Users/MR_Salman/Desktop/Sal.txt");
	
	

char scores ;
while (!file1.eof ())
{
file1 >> scores;
file2 << scores;
}
file1.close();
file2.close();
return 0;
}
Last edited on Jun 24, 2014 at 6:55pm
Jun 25, 2014 at 8:40am
Thanks Salman. The code worked, but it copied the input file simply as characters. The contents of the input file are matrices like this:
10 20 30 40 50
20 30 40 50 60
30 40 50 60 70

I need to copy it and save them on the output file as same.
Jun 25, 2014 at 8:47am
int $numBERS [20]; $ at the beginning of name is illegal. However GCC has extension which allows it.

getline (infile, $numBERS [20]); Out of range access (you are trying to write to 21th array cell) and getline does not work with integers.

It would be nice if you post error messages if any, as it is clearly another problem, not one we were trying to solve at the beginning.
Jun 25, 2014 at 8:50pm
Sorry i can't get you ..... If you wanna say that you have integers in file and i am reading file as characters ??? Is that the problem???
If yes then This is not a problem ......You should not confuse yourself
The above logic will work just fine .. You are just copying files ....
Last edited on Jun 25, 2014 at 8:53pm
Jun 25, 2014 at 8:52pm
Here i have another way if it could help you


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <fstream>

using namespace std;


int main()
{

ifstream infile;
ofstream outfile;
infile.open ("C:/Users/MR_Salman/Desktop/ugo.txt", ios::in);
outfile.open ("C:/Users/MR_Salman/Desktop/Book.txt", ios::out);

string scores;
int numBERS [20] , i = 0 ;
while (!infile.eof ())
{
infile >> numBERS[i] ;
if ( i%5==0&&i!=0 )//To insert new Line after five numbers
{
	outfile<<"\n" ;
}
outfile << numBERS[i] << " ";

i++ ;
}

return 0;
}
Jun 25, 2014 at 8:56pm
...and never loop on .eof() member function.
file and output:
1
2
3
1 56 78 65 34
42 91 105
1 56 78 65 34
42 91 105 105
Jun 25, 2014 at 9:33pm
oh realy i didn't consider that mistake ......
Thank you ...
But will you plz tell me what could be the possible solution and what will be replacement for eof()???
plzzzz...
Jun 25, 2014 at 9:44pm
1) loop on input operation:
1
2
3
while( infile >> numBERS[i] ) {
  //rest of code
}


2) check stream state after read
1
2
3
4
5
6
while(true) {
    infile >> numBERS[i];
    if( !infile ) //Unsuccesfull read
        break; 
    //rest of code
}
you can also check if loop was terminated because you hit end of file or encountered an error after the loop:
1
2
3
4
if (infile.eof())
    std::cout << "file is completely read";
else
    std::cout << "There was an error...";


Another bonus of this code is that it captures not only end of file but other errors too, preventing possible infinite loop.
Example of infinite looping:
1
2
3
4
5
std::istringstream in("Hello, world!");
int i;
while(!in.eof()) { //infinite loop
    in >> i; //fails without reading a single character. eof() is not  set
}
Last edited on Jun 25, 2014 at 9:47pm
Jun 25, 2014 at 9:59pm
Thanks bro
Really Thank You
Jun 29, 2014 at 3:57pm
Thanks Salman0349. The problem has been solved. The code was optimized like this:

#include <iostream>
#include <fstream>

using namespace std;


int main()
{

ifstream infile;
ofstream outfile;
infile.open ("Filename1", ios::in);
outfile.open ("Filename2", ios::out);

string file_Header;
int myArray [n], i = 0;
//while (!infile.eof ())
getline (infile, file_Header);
outfile << file_Header << endl;

for (i=0; i< n; i++)
{

// print the current element...
infile >> myArray [i];
outfile << myArray [i] << '\t';

// we make a new line only after elements 4,9,14,19,24...

if ((i % 5) == 4)
{
outfile << endl;
}
}

infile.close();
outfile.close();
outfile << flush;

return 0;
}
Last edited on Jun 29, 2014 at 3:57pm
Topic archived. No new replies allowed.