|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Resident AMD enthusiast
Join Date: Jul 2001
Location: Kansas
Posts: 1,445
|
read loops
My language of choice: C++
![]() What is the proper sequence for a read loop? I believe according to my teacher (who has been absent the last two days. . .) the proper way to make a read loop is: Code:
// horrible pseudo code below. . . but it gets the point across
read >> data
while (!eof)
{
array[i] = data
i++
read >> data
}
So if the problem is that the loop is storing, incrementing, and then reading, I thought I'd try this: Code:
while (!eof)
read >> data
i++
array[i] = data
![]() I have been having nightmares about this problem, I am out of ideas for how to do a good read loop. Any suggestions? Also, I want to experiment with config files so I don't have to re-compile my program when I'm debugging and there is a line i don't want to run. I want my config files to be human readable, so I'd like to have something like: Code:
sort: t output: 3 blah. . . Code:
read char store into "sort" ( <<-- variable where char is stored would never change) read num store into "output" Thanks in advance for any input about solving these problems. L J
__________________
Main: Gigabyte GA-770T USB3 - Phenom II 840 - 4GB DDR3 - Radeon 5750 1GB HTPC: MSI K9N6PGM2-V2 - Athlon II 250 - 4GB DDR2 - Radeon 5670 512MB HTPC: Zotac GeForce 6100E-E - Athlon X2 5800+ - 4GB DDR2 "Play a Windows CD backwards and you'll hear satanic voices, thats nothing, play it forwards and it installs Windows." Last edited by Colonel Sanders; 12-08-2006 at 11:33 PM. |
|
|
|
|
|
#2 |
|
Barefoot on the Moon!
Staff
Premium Member
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
|
ok, I'm not too familiar with C++ syntax, but I am familiar with the problem.
Here's bascially how it goes: first, read the file into a variable outside of the loop. In the while loop statement, check to see if that variable is null. If not null, continue into the loop. store the value of the temporary variable where you want it (in this case, you want it in the array, I assume). Increment the counter (i) Read again into the temporary variable, then go back up to the while statement. If the temporary variable is not null, continue, else break out of the loop. This is an example of java/c# code (again, I'm not familiar with c++, so I hope this helps) Code:
int i = 0;
string[] myArray = new string[50]; //or however many array spaces you think you need...be generous
string newLine = myFile.ReadLine(); //or read char, or whatever you need here
while(newLine != null){
myArray[i++] = newLine;
newLine = myFile.ReadLine();
}
As for the reading the config file, throw in an if statment that says if the string equals "" (or char equals ''), continue to the next line. Null is only thrown when you reach the end of the file. A note about this though...is this a "band-aid fix" on a problem, or part of an actual solution?
__________________
There are two secrets to staying young, being happy, and achieving success. You have to laugh and find humor every day, and you have to have a dream.
Last edited by Force Flow; 12-09-2006 at 07:42 PM. |
|
|
|
|
|
#3 |
|
Member (9 bit)
Join Date: Feb 2005
Posts: 392
|
use std::getline function, it will read one line at a time.
ie: string str; getline(in,str); // Get the frist line from the file, if any. while ( in ) { // Continue if the line was sucessfully read. processLine(str); // Process the line. getline(in,str); // Try to get another line. } in "processLine" you can look for "sort" "t" so on...by parsing the string anyway you wanted. of course, you have to check for eof with : " if(in=eof) then....". alternatively, you can do it by creating your own "readline" procedure and check for the end of each line by searching for "\n" character.
__________________
words to live by: others don't know, I know. others know, I know more. others know more, I excel. one shouldnt read this far; above, is meant as an encouragement, translated from a Chinese Proverb. "He who angers you conquers you." : Elizabeth Kenny |
|
|
|
|
|
#4 | |
|
Member (13 bit)
Join Date: Mar 1999
Posts: 6,791
|
Quote:
|
|
|
|
|
|
|
#5 | |
|
Barefoot on the Moon!
Staff
Premium Member
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
|
Quote:
Thanks, I fixed it.
|
|
|
|
|
|
|
#6 |
|
Member (7 bit)
Join Date: Feb 2004
Location: Owings Mills, MD
Posts: 95
|
I haven't coded for a while in C++ but I think I know your problem.
Use a "do...while" instead of a "while..." loop. You're not storing the last line because your code first check the end of the file and then performs. So it works like this: 1- check if end-of-file = false 2- store data in variable from previous read 3- jump to next line 4- read file (last one in file) ...begin loop again 1- check if end-of-file = true Stops! see, you read the last line but it didn't get the chance to store it because the file was already at the end. on a do... while loop it checks end-of-file AFTER it performs all operations Code:
do {
array[i] = data
i++
read >> data
} while (!eof)
|
|
|
|
|
|
#7 |
|
Resident AMD enthusiast
Join Date: Jul 2001
Location: Kansas
Posts: 1,445
|
According to the teacher the problem was simply that he forgot to add an blank line to the end of the file. Array[i] would still return a garbage value, but so long as I use loops that use less "while < i" then I am fine.
L J |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| XP won't read CD data!! | MaximumDMA | Computer Hardware | 9 | 11-03-2005 08:55 PM |
| Read Only Properties? | nightsky | Windows Legacy Support (XP and earlier) | 1 | 09-19-2005 04:11 PM |
| Can only read 1 disc per boot ? | KoMoDo | Computer Hardware | 8 | 02-27-2005 05:13 PM |
| LG DVD Rom: can't read DVD discs | Jova | Computer Hardware | 6 | 12-03-2002 07:37 AM |
| read only files ????? | Richard M Balling | General Discussion | 3 | 11-04-2002 03:53 PM |