![]() |
|
#1
|
|||
|
|||
Importing data from an ASCII fileHey all,
Suppose i have an ASCII file in which each line contains there values x, y and z separated by a blank space. Say its a list of vectors for examples. to import it into a c++ i normally would use the following code: CPP / C++ / C Code:
This is fine as long as the data being imported is numerical, but suppose i have the following scenario: the ASCII file to be imported is a basic database in which each line contains a products reference number, price and product description. The price is a floating point number but the reference number and product descriptions are strings. The matter is complicated further by the fact that the product description can contain blank spaces and can be anything up to (say) 55 characters long. How would i modify this code to be able to import the data from the database into a c++ program? Seeing as character strings are treated as arrays in themselves would i have to create a class to house multiple product descriptions? Thanks, Chopo Last edited by admin : 04-Apr-2007 at 00:13.
Reason: Please insert your C code between [cpp] & [/cpp] tags
|
|||
|
#2
|
||||
|
||||
Re: Inporting data from an ASCII fileRead the entire line into a string. Then look at the string and move to permanent storage
1) the first part of the string (reference number) character by character until you get to the first character that's not a reference number character. 2) Skip the next characters until you hit a price character. 3) Convert to float 4) Skip until you get past the number and to the first character of the description. 5) Move the rest of the string to the description storage. __________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#3
|
|||
|
|||
Re: Inporting data from an ASCII fileThanks WaltP
Ok, here is my modified program according to your, instructions. I have neglected converting the price to float for now. Whilst it compiles there appears to be a bug as if i then create a for loop to output the strings on screen i find that the whole line appearers to be turned into an integer value and only the corresponding character to this number is stored. Could you tell me what i am doing wrong: CPP / C++ / C Code:
Last edited by admin II : 04-Apr-2007 at 07:43.
Reason: Please surround your CPP code with [CPP] ... [/CPP]
|
|
#4
|
|||
|
|||
Re: Inporting data from an ASCII fileAlso, is there a command to count the number of lines within an ASCII file so that a c++ program can know how large an array it has to create to store all the data within the ASCII file.
|
|
#5
|
||||
|
||||
Re: Inporting data from an ASCII fileQuote:
What is this line supposed to do? ref[i] = (line, 0, 6); I ask because it does nothing. At least nothing you need done. All you did was state the variable and 2 integers. Did you forget a substring or move command? CPP / C++ / C Code:
Other than that, as long as your data is formatted exactly, this technique will work fine. __________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#6
|
|||
|
|||
Re: Inporting data from an ASCII fileWith respect to the n=0, i don't think that was the problem, assume its set to however many lines there are in the ASCII file. I left the N=0 there by accident because there originally was a loop in the code that tried to count how many lines there are in the file:
Code:
But this just caused me more problems rather than counting how many lines there were in the document. as for: CPP / C++ / C Code:
CPP / C++ / C Code:
So here is my amended code: CPP / C++ / C Code:
Ofcource it still does not work because as you pointed out ref[i] (line, 0, 6); does not actually take six characters out of string line starting from character 0 nore do the two following lines. I think i'm suppose to use the get, getword or some related function but do not know how to specify which characters out of the string line should be transfered to variables ref[i], val[i], and dscrpt[i]. Also, is there a command to count the number of lines within an ASCII file so that a c++ program can know how large an array it has to create to store all the data within the ASCII file. |
|
#7
|
||||
|
||||
Re: Inporting data from an ASCII fileQuote:
Think about it this way. You want a box that can hold balls. N is used to define how many balls fit in the box. You set N to zero, grab a box that can hold N balls. How big is that box? You need to define your string to hold the proper number of values. You can't base it on N. Quote:
Quote:
Quote:
__________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#8
|
|||
|
|||
Re: Inporting data from an ASCII fileCPP / C++ / C Code:
At that point, tokens[0] contains the first item, tokens[1] the second, and tokens[3] the third. Move/convert them from there. As for the loop, why would you not read until the end of the file? |
|
#9
|
|||
|
|||
Re: Inporting data from an ASCII fileQuote:
I wanted to count the number of lines in the file before running through it in order to know how large the arrays have to be in order to store all the entries in the ASCII file. Thanks for the code by the way |
|
#10
|
|||
|
|||
Re: Inporting data from an ASCII fileWaltP, you are giving to much importance to the N bug. I know that i need to set a value for N in order to get the loop to work, i was just trying to keep the example generic, the code i am actually working on is effectively identical but has more arrays and some other stuff which was not relevan to my question. Seeing as this has caused confusion i will assume N=85 from now on.
|
Recent GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 08:44 |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 14:12 |
| CD burner wont burn!! | robertli55 | Computer Hardware Forum | 1 | 18-Jun-2004 11:53 |
| [CONTEST?]Data Structure Test | dsmith | C Programming Language | 2 | 06-Jun-2004 16:13 |
| Yet another CD burner problem: Lite-On LSC-24082K | Erwin | Computer Hardware Forum | 1 | 22-May-2004 12:28 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The