C++ - Reading string from a text file

Status
Not open for further replies.

pushkar

Journeyman
I have to read a text file using ifstream. The file contents are

Code:
1
10
Insurgence
1998
7.1
0

I have written following two codes for reading the file

code 1

Code:
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char c[100];
ifstream fin("MOVIE.DOC");
int n;float m;
fin>>n;
cout<<n<<endl;
fin>>n;
cout<<n<<endl;
fin.getline(c, 100, '\n');
cout<<c;
cout<<endl;
fin>>n;
cout<<n<<endl;
fin>>m;
cout<<m<<endl;
fin>>n;
cout<<n<<endl;
getch();
}

code 2

Code:
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
ifstream fin("MOVIE.DOC");
char c[100];
while(!fin.eof())
{
fin.getline(c,100,'\n');
cout<<c<<endl;
}
getch();
}

The second code is working, but the first one isn't giving the desired output. In the first code, the third line, which is the string, isn't giving proper output.

I cannot use the second code, because I have to store everything except the third line in int or float types, while in the second code, i am storing everything in strings.

Please advise me what is the problem.

BY THE WAY, I AM USING TURBO C++ 3.0. Can this be causing any problem?
 
yeah turbo c++ 3.0 might be the source of error but i would suggest you to create a class having the values that you have to read as its data members then every thing will be read and stored in the object in the respective data members however in this method you will have to specify a size of the data members especially string and hence cause wastage of memory.

Your code is efficient enough to handle the data.

and one probable cause of the error may that you have assumed the string (movie name to be of constant size 100 characters) but after the string "insurgence" the program should stop reading but its instructed to read upto 100 characters and it simply crosses the new line characters unnoticed.

remember there is no bound checking in case of C and C++ that means that for an array arr of size six you can write arr[10] and the compiler does not show any error. the error that occurs during runtime and the pointer starts pointing to some garbage value.

same thing is happening in your case the file pointer has moved out of bounds.

try reading the characters one by one till you do not encounter a \n and then save it in a string . this will solve your problem.

the code might be something like this

int i=0;
char str[100], ch;
while (ch != '\n')
{
ch = fin.getc();
str= ch;
i++;
}

8)

hope it helps
 
Last edited:
OP
pushkar

pushkar

Journeyman
what output are you getting ?? maybe then someone can help.
I am getting output like this

1
10
_ (Cursor)

24%232
184

The third line is blank. The fourth and fifth lines are showing some junk values.

@anuvrat, I don't think that using a character array of more size than required is causing the problem, because I have given the delimiter in the getline function as '\n'. If that was the problem, the second code should also not work.

I think it is the problem of TC++. I will try with another compiler.
 
the second code is something that reads the whole file into a buffer

hey wait wait ...
the file is binary file or a text file? most probably text file isint it?

now in this case you are trying to read input into an integer variable when its opened in text mode?
may be this is causing the trouble

but if its so the first two outputs should not be 1 and 10.
 
OP
pushkar

pushkar

Journeyman
I finally got it to work. The problem was indeed with Turbo C++. I used Relo along with Borland C++ compiler. I inserted the exact same code and now the output is correct.

Time to ditch TC.

Edit: Again the same problem. I didn't modify anything and now again I am not receiving the correct output.

The output is like this

1
10

0
0
0

while the output should be

1
10
Insurgence
1998
7.1
0
 
Last edited:
Status
Not open for further replies.
Top