Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 27-01-2009, 11:26 PM   #1 (permalink)
Broken In
 
pushkar's Avatar
 
Join Date: May 2006
Posts: 196
Default C++ - Reading string from a text file


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?
pushkar is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 27-01-2009, 11:46 PM   #2 (permalink)
In The Zone
 
vandit's Avatar
 
Join Date: May 2005
Location: If I had a webcam u could have seen me, before your eyes.
Posts: 458
Default Re: C++ - Reading string from a text file

what output are you getting ?? maybe then someone can help.
__________________
Searching for dell studio 15 ??
dell studio 15 review @ my blog

vandit007.blogspot.com

And remember the stylus that comes along with the touchscreens isnt meant for cleaning your ears !!
vandit is offline  
Old 28-01-2009, 12:41 AM   #3 (permalink)
paralinux
 
Join Date: Jul 2008
Location: Noida
Posts: 86
Default Re: C++ - Reading string from a text file

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[i]= ch;
i++;
}

8)

hope it helps

Last edited by anuvrat_parashar; 28-01-2009 at 12:47 AM. Reason: Automerged Doublepost
anuvrat_parashar is offline  
Old 28-01-2009, 02:42 PM   #4 (permalink)
Broken In
 
pushkar's Avatar
 
Join Date: May 2006
Posts: 196
Default Re: C++ - Reading string from a text file

Quote:
Originally Posted by vandit View Post
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.
pushkar is offline  
Old 28-01-2009, 03:54 PM   #5 (permalink)
paralinux
 
Join Date: Jul 2008
Location: Noida
Posts: 86
Default Re: C++ - Reading string from a text file

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.
anuvrat_parashar is offline  
Old 29-01-2009, 03:03 PM   #6 (permalink)
Broken In
 
pushkar's Avatar
 
Join Date: May 2006
Posts: 196
Default Re: C++ - Reading string from a text file

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 by pushkar; 29-01-2009 at 11:48 PM. Reason: Automerged Doublepost
pushkar is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
A 5 gb text file!! shady_inc QnA (read only) 7 07-12-2007 08:44 PM
Text file in 6030 New Mobiles and Tablets 3 21-08-2007 01:13 PM
File reading error in memory card in Ngage casual_gamer Mobiles and Tablets 9 30-01-2007 05:23 PM
Add text to video file...how? linardni Software Q&A 13 25-09-2006 09:14 PM
converting .pdf to text file abhijitnandedkar Software Q&A 2 29-12-2005 11:42 PM

 
Latest Threads
- by gforz
- by soumya
- by Sujeet
- by icebags
- by Charan

Advertisement




All times are GMT +5.5. The time now is 03:06 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2