| bbesase (21) | |||||
Ok so I have this project for school in which I have to read in a .txt file into an array then spit it out into a bubble sorter and sort it accordingly. I have the program opening the file, and reading the array in and printing out the array. But I'm not sure how to incorporate a delimiter. I think this is somewhat it, maybe?
This is my code for the int main so far. I'm not too sure where to put the delimiter part. Then on top of all of that I still have to incorporate the sorter.
I hate how everything is in the int main{} also, although we havent learned about different functions in class yet. Thanks in advance for the help! | |||||
|
|
|||||
| Lynx876 (561) | |
|
getline, by default has a delimiter of '\n'( new line ) If you want something else, try: getline( iFile, Line, ',' ); etc.The third parameter being the delimiter. | |
|
Last edited on
|
|
| bbesase (21) | |
So if the .txt file he gives us is serperated by |'s then I would just putgetline( iFile, Line, '|');Is it really that easy? | |
|
|
|
| Lynx876 (561) | ||||||
|
Yeah! Until you hit the end line( '\n' ). When I read multiple variables, from one line within a text( or other )file. I tend to collect the whole line in one loop. i.e. Sample text file:
Edit: These are the same!
| ||||||
|
Last edited on
|
||||||
| bbesase (21) | |
| Ok. So now I'm running into the problem of when it goes through the delimiter it stores every value that was seperated by the "|" and puts them onto a seperate line in the array. however, I'm not supposed to keep every bit of info that he gave us, only 5 of the 13 fields... I don't know how to make it so I just hit certain values. | |
|
|
|
| Lynx876 (561) | |||
If you know which fields you need...
Edit: If you don't need the first, say 5, fields. Maybe use a for-loop to get to where you need to be. | |||
|
Last edited on
|
|||
| bbesase (21) | |
| Well that's the thing. 3 fields are strings, and the other 2 are integer's. Do I have to declare variables in the beginning of the function in order to set it up correctly? And another thing the variables wont be the same every time, they change every line. | |
|
|
|
| LowestOne (895) | ||||||||
Do you mean the format changes?
If so, then the best bet is to read everything as a string, and then figure out which ones are numbers later. If you have
Then I would set up something just for that file:
| ||||||||
|
|
||||||||
| Lynx876 (561) | |||
Using getline, in the current way, only reads in to a sting. To copy the string to an integer, you may want to look up "stringstream".
And if the fields are going to move around. You may want to read the whole line in to a string getline( iFile, input, '\n' ); then parse it first.
| |||
|
|
|||
| bbesase (21) | |
| Hmm alright, thank you so much! I will work with it and see if I can get it working lol. | |
|
|
|
| bbesase (21) | |
|
LowestOne: They are str | str|int| int| int| ... so on, but every line the numbers and strings are different So they same string and numbers wont be there every line. | |
|
|
|
| Lynx876 (561) | ||||||
|
Ohhh! 'input' is just a temporary holder of the data! say you have a struct:
Then, each getline, you'd just save to here!
Same sample text file as before:
| ||||||
|
Last edited on
|
||||||
| LowestOne (895) | |
|
You want a container of classes. Have you learned about vectors or classes? If so, each line is a class. For each line, you instantiate a class and push it on to your vector. Otherwise... You need a bunch of parallel arrays which, in the best case, are sized to the amount of lines in your file. If you don't know about dynamically allocating arrays, and you feel your teacher might use a different file than yours as data, then make the arrays much larger than you will need. | |
|
|
|
| bbesase (21) | |
|
Lynx, they aren't inputs, they are values of a .txt file. here is what each line looks like. 1061698|OH|Torch|ppl|Athens|39|009|391422N|0814457W|39.23944|-81.74917|||||730|||Lubeck I need the State, City, latitude, longtitude and one other one. | |
|
|
|
| mezmiro (17) | |
| Okay.. what's with the 5 '|''s? Seems like your teacher is screwing with you, unless those fields are allowed to be empty. =/ The previous advise still seems to apply though. Parse it as a string and when you encounter a '|' then break it into another string in a loop structure. Though.. I can't easily identify the structure the way you described it. | |
|
|
|
| Lynx876 (561) | |||
Technically, they are inputs, from a file. But... You're taking too much notice of the name 'input'. Just remember, it's just a temporary variable!
| |||
|
|
|||
| bbesase (21) | |
|
Ok I see what your saying now, So what would I do if I needed to skip certain fields? Sorry I'm new at this programming thing, I don't mean to keep asking you the same stuff over and over lol... But this is a great help. Where are you getting the students[i].name from?
| |
|
|
|
| Lynx876 (561) | ||||
|
The array of students came from an earlier post. Have a look through this. Edit: Don't worry about all the std::'s. It's because I don't include using namespace std;
This in the infile:
| ||||
|
Last edited on
|
||||
| bbesase (21) | |
Ohh, ok now it makes sense what your doing. I didn't know you could just tell it not save a certain chunk of info. So can I just put as manygetline( iFile, input, ',' );as I need then? | |
|
|
|
| Lynx876 (561) | |
| Yes. Then each time you re-use the getline statement, 'input' will just be re-written with the next chunk of data that's being read in. (: | |
|
|
|