Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 25th, 2008, 11:45 PM   #1
ShawnStovall
Call me Chuck
 
ShawnStovall's Avatar
 
Join Date: Aug 2007
Location: USA, MI
Posts: 37
Rep Power: 0 ShawnStovall is on a distinguished road
Send a message via AIM to ShawnStovall
Reading text from a file.

I am having a problem reading text from a file. I thought I have followed every rule for opening and reading a file but it is still not working. Note, I am not completely done with this, I still need to add some precautionary code for bad files. The problem is that the program seems to have problems opening the file and I can't figure out what is wrong, here is the source:

C++ Syntax (Toggle Plain Text)
  1. // main.cpp
  2. #include <iostream>
  3. #include "QuizClass.h"
  4. #include <fstream>
  5. using std::cout;
  6. using std::ifstream;
  7. using std::endl;
  8.  
  9. int main()
  10. {
  11. ifstream tFile("test.txt");
  12. CQuiz test;
  13.  
  14. tFile >> test;
  15.  
  16. test.askques();
  17.  
  18. return 0;
  19. }
C++ Syntax (Toggle Plain Text)
  1. std::ifstream& operator>>(std::ifstream& inFile, CQuiz& obj)
  2. {
  3. std::string temp;
  4. if(!inFile)
  5. {
  6. std::cout << "\nThere has been an error opening the file.\n";
  7. return inFile;
  8. }
  9. while(!inFile.eof())
  10. {
  11. std::getline(inFile, temp);
  12.  
  13. if(temp[0] == '#')
  14. {
  15. temp.clear();
  16. }
  17. else if(temp[0] == '$')
  18. {
  19. int j = 0;
  20. string subt;
  21.  
  22. for(int i = 1; i == temp.size(); i++)
  23. {
  24. subt[j] = temp[i];
  25. j++;
  26. }
  27.  
  28. obj.addques(subt);
  29. temp.clear();
  30. }
  31. else if(temp[0] == '~')
  32. {
  33. std::string subtemp;
  34. int j = 0;
  35.  
  36. for(int i = 1; i == temp.size(); i++)
  37. {
  38. subtemp[j] = temp[i];
  39. j++;
  40. }
  41.  
  42. obj.addpos(subtemp);
  43. temp.clear();
  44. }
  45. else if(temp[0] == '@')
  46. {
  47. int j = 0;
  48. string subt;
  49.  
  50. for(int i = 1; i == temp.size(); i++)
  51. {
  52. subt[j] = temp[i];
  53. j++;
  54. }
  55.  
  56. obj.isans(subt);
  57. temp.clear();
  58. break;
  59. }
  60. else
  61. {
  62. std::cout << "\nLine not computable: \"" << temp << "\" Please check your code. Continuing normaly...\n";
  63. temp.clear();
  64. }
  65. }
  66. return inFile;
  67. }

Thanks for your time, sorry if I didn't explain well enough.
__________________
sqrt(-1) <3 3.14

98% of the teenage population will try, does or has tried smoking pot. If you're one of the 2% who hasn't, copy & paste this into your signature
ShawnStovall is offline   Reply With Quote
Old May 26th, 2008, 7:48 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,254
Rep Power: 5 grumpy will become famous soon enough
Re: Reading text from a file.

Most obvious thing to check: does the file exist?

A common occurrence is for the current working directory differs from the directory that the file ("test.txt") is in - i.e. the file is not in the current working directory.
__________________
Dear God
So far today I've done all right. I haven't been grumpy yet. But in a few minutes, God, I'm going to get out of bed, and from then on I'm going to need a lot more help.
AMEN
grumpy is offline   Reply With Quote
Old May 26th, 2008, 9:40 AM   #3
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Re: Reading text from a file.

I usually put the text file in the same directory as the executable (\Debug for VS) and it finds it without a full path. But it would make your life easier if you specify the full path of the file (make sure to escape \ with \\).
OpenLoop is offline   Reply With Quote
Old May 26th, 2008, 10:09 AM   #4
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 104
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
Re: Reading text from a file.

Quote:
for(int i = 1; i == temp.size(); i++)
^
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote
Old May 26th, 2008, 11:17 AM   #5
ShawnStovall
Call me Chuck
 
ShawnStovall's Avatar
 
Join Date: Aug 2007
Location: USA, MI
Posts: 37
Rep Power: 0 ShawnStovall is on a distinguished road
Send a message via AIM to ShawnStovall
Re: Reading text from a file.

Hmmm... I've checked to make sure the file exists, as a matter of fact I wrote it by hand. I did put it in \debug like I'm supposed to but I'm still getting my error. Perplexing...
__________________
sqrt(-1) <3 3.14

98% of the teenage population will try, does or has tried smoking pot. If you're one of the 2% who hasn't, copy & paste this into your signature
ShawnStovall is offline   Reply With Quote
Old May 26th, 2008, 2:44 PM   #6
ShawnStovall
Call me Chuck
 
ShawnStovall's Avatar
 
Join Date: Aug 2007
Location: USA, MI
Posts: 37
Rep Power: 0 ShawnStovall is on a distinguished road
Send a message via AIM to ShawnStovall
Re: Reading text from a file.

I have it working right but the problem is I can't get the getline() function to read a whole line into the temp object. Any help would be appreciated, thanks.
__________________
sqrt(-1) <3 3.14

98% of the teenage population will try, does or has tried smoking pot. If you're one of the 2% who hasn't, copy & paste this into your signature
ShawnStovall is offline   Reply With Quote
Old May 26th, 2008, 3:58 PM   #7
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 311
Rep Power: 3 Fall Back Son is on a distinguished road
Re: Reading text from a file.

did you read the for loop peaceofpi quotes? its wrong. lol
Fall Back Son is offline   Reply With Quote
Old May 26th, 2008, 4:16 PM   #8
ShawnStovall
Call me Chuck
 
ShawnStovall's Avatar
 
Join Date: Aug 2007
Location: USA, MI
Posts: 37
Rep Power: 0 ShawnStovall is on a distinguished road
Send a message via AIM to ShawnStovall
Re: Reading text from a file.

AHH!! I'm so stupid. lol That is too obvious! I'll fix it and come back with the results.
__________________
sqrt(-1) <3 3.14

98% of the teenage population will try, does or has tried smoking pot. If you're one of the 2% who hasn't, copy & paste this into your signature
ShawnStovall is offline   Reply With Quote
Old May 26th, 2008, 5:02 PM   #9
ShawnStovall
Call me Chuck
 
ShawnStovall's Avatar
 
Join Date: Aug 2007
Location: USA, MI
Posts: 37
Rep Power: 0 ShawnStovall is on a distinguished road
Send a message via AIM to ShawnStovall
Re: Reading text from a file.

It is now working just as I wanted! Thanks everyone.
__________________
sqrt(-1) <3 3.14

98% of the teenage population will try, does or has tried smoking pot. If you're one of the 2% who hasn't, copy & paste this into your signature
ShawnStovall is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading file - convert ASCII to characters Matt42 C++ 8 Jan 2nd, 2008 6:49 PM
Adding more info to a text file without erasing crawforddavid2006 C# 2 Apr 11th, 2007 3:10 PM
Reading contents of text file then printing out, not working. chillypacman PHP 5 Jun 19th, 2006 7:29 AM
reading a random line from a text file cloud- Visual Basic 11 Apr 8th, 2005 2:34 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 5:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:56 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC