![]() |
|
#1
|
|||
|
|||
Need help reading data from filesI've been trying to fix this problem for the past several hours but it just gets worse everytime I try.
The program is supposed to read data in from the file "Snow.data" which contains data in this format: Code:
One part of the assignment requires a switch statement to convert the character values (C, G, L, N, O) to a real description (cherry, grape, lime, coconut, orange) and a function to read in the location number and number of different flavors sold at the location (#'s on first line). My switch statement works and I'm able to read in the correction location ID. However, the program does not print the correct flavors with the locations. The data printed overflows from the next location. Sample Output: Code:
Here's my C coding: CPP / C++ / C Code:
Can someone please help me? I'm at my wit's end! Last edited by LuciWiz : 15-Mar-2005 at 13:59.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#2
|
|||
|
|||
|
Here, this works for me. I think one of your main problems is that I don't think the scanf() family of functions reads along a string/file like one would with fgets() or fread(), but rather scans the from the start each time. My solution, a not so neat one I might add, is to create a function to crawl along the string and pick up a char and an int after it, ignoring all whitespace. There were also a few other miscellaneous bugs which I noticed, and there might be a few more that I missed, but this works for now (at least, I think it does).
CPP / C++ / C Code:
Sorry it's not very neat. |
|
#3
|
|||
|
|||
|
Dr. Evil,
Thanks for your reply! Unfortunately, we haven't covered fgets or arrays yet in the class, so I cannot use that in the program. I'm limited to using plain old fscanf and fprintf. That's why I'm having so many problems! As soon as I can get this error fixed, I hopefully get the other functions to calculate correctly. Code:
I just don't understand why the first block of data from the file was read and printed correctly, but after that, everything else is messed up. I'm so frustrated right now I'd pay someone to write the entire program for me! LOL ![]() |
|
#4
|
|||
|
|||
|
How's this then? I was wrong about fscanf(), it does go along the file. One of your big problems was the <= in the for() loop, it should have just been <. What I also added was something to read through until it didn't come across a space, otherwise it would accept the space as the char.
CPP / C++ / C Code:
Also kind of messy... |
|
#5
|
|||
|
|||
|
Quote:
The return value from scanf() or fscanf() tells you how many items in your specification were actually read. If you know that there are supposed to be three ints, you can simply do something like this: CPP / C++ / C Code:
You can do the equivalent with fscanf() --- and you should always (always) check the return value of scanf() and fscanf(). In your case, you need to know the exact format of the input data before you can write a program to get the data into your data base. Some lines have int int Some lines have char int char int int int Some lines have char int char int char int int int ... I ask myself: what do these mean? Forget the program for a minute; what do they mean to me? More importantly, what do they mean to you? Can you predict how many things of each type are on each line? Whether you use fscanf() or fgets(), unless the format is fixed and required to be in specific form, you will have to test the data as you go, and decide what is what and where it goes. Before you write a program: make sure you know what the input is supposed to be. Write out the input specification and see if you can see any way to implement the function to read the input data. (And as you implement the code, make sure you can detect bad input and either attempt to recover, or, at the very least, report errors before aborting execution.) Regards, Dave |
|
#6
|
|||
|
|||
|
*sighs* Thanks for helping Dr. Evil and David. I think I'm just going to give up on this program. I doubt that I'll have it figured out by tomorrow morning at this rate; I've been working on it all weekend.
|
Recent GIDBlog
Toyota - 2008 September Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 13:12 |
| Need Help reading HEX data from a sensor | Moooey | C Programming Language | 4 | 09-Mar-2005 13:46 |
| [CONTEST?]Data Structure Test | dsmith | C Programming Language | 2 | 06-Jun-2004 15:13 |
| reading a char* into struct data | spike666 | C Programming Language | 7 | 19-Apr-2004 12:06 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The