Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 12-08-2006, 11:31 PM   #1
Resident AMD enthusiast
 
Colonel Sanders's Avatar
 
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 
  }
The problem I am finding with this is that the loop is continuous- it stores data and then reads the next piece of data- the last run of the loop does not store the last read piece of data into the array so with the above loop I end up with a null value in array[i], the 2nd to last piece of data gets stored in array[i-1], and the last record is still in the variable data. I have found that if a add "array[i] = data" after the while loop the array gets all the values I need in the proper locations- but I don't really like that solution 'cause it seems cheap and I think/hope there is a better way to 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
But that give me a null value for array[0]

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. . .
I would then need a way to store t into the variable sort so that my sort will run. The only way that I can think of to implement config file reading would be like

Code:
read char
store into "sort" ( <<-- variable where char is stored would never change)
read num
store into "output"
The problem with that approach is if I edit the config file and remove the "sort: t" line, or I simply move that line than the read won't know where to store the value, I'd like to use the config file to determine what to store, and where to store it. And what if I have a blank line? like "out: " - how can I make the read move to the next line?

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.
Colonel Sanders is offline   Reply With Quote
Old 12-09-2006, 09:25 AM   #2
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
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.
Force Flow is offline   Reply With Quote
Old 12-09-2006, 03:59 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
alfie2 is offline   Reply With Quote
Old 12-09-2006, 07:28 PM   #4
Member (13 bit)
 
Floppyman's Avatar
 
Join Date: Mar 1999
Posts: 6,791
Quote:
Originally Posted by Force Flow
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 = aFile.ReadLine();
}
?
Force, either I'm blind or your code is not actually storing any data in the array....
Floppyman is offline   Reply With Quote
Old 12-09-2006, 07:39 PM   #5
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
Quote:
Originally Posted by Floppyman
Force, either I'm blind or your code is not actually storing any data in the array....
Whoops...it isn't. That's what I get for using a textbox and not a complier. Thanks, I fixed it.
Force Flow is offline   Reply With Quote
Old 12-11-2006, 03:44 PM   #6
Member (7 bit)
 
Zick Boy's Avatar
 
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)
I hope this helps!
Zick Boy is offline   Reply With Quote
Old 12-12-2006, 12:07 AM   #7
Resident AMD enthusiast
 
Colonel Sanders's Avatar
 
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
Colonel Sanders is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT -5. The time now is 05:17 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2