6 Replies - 641 Views - Last Post: 29 September 2010 - 05:12 AM Rate Topic: -----

#1 beefstew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-August 10

I don't understand the concept of file reading

Posted 28 September 2010 - 03:09 AM

Hello, say I have a file named hw4.data and its contents are,
Aerospace CSci 230 3.0
Aerospace CSci 230 4.0
Aerospace CSci 230 2.0
Aerospace CSci 451 3.0
Aerospace CSci 451 2.0

How can I read the file and store things accordingly, for example say I have an int array[], how can I read the file so it stores things like array[0]= 230
array[1]=230 array[2]=230 array[3]=451 array[4]=451. Or if I had a float array2[], how can I read the file so it stores things like array2[0]=3.0 array2[1]=4.0 array2[2]=2.0 array2[3]=3.0 array2[4]= 2.0 etc, how does it distinguish an int from a string from a float, etc?

also, say the file contents looked like this now
Aerospace CSci 250 3.0
Aerospace CSci 230 4.0
Aerospace CSci 220 2.0
Aerospace CSci 451 3.0
Aerospace CSci 450 2.0

Say I somehow way able to scan the file and store the int elements in the int array[], how can I take that int array[] and use its contents to sort the file outputs, example,
Aerospace CSci 220 2.0
Aerospace CSci 230 4.0
Aerospace CSci 250 3.0
Aerospace CSci 450 2.0
Aerospace CSci 451 3.0

what if the file looks like this,
230 Aerospace CSci 3.0
Aerospace 230 CSci 4.0
Aerospace CSci 2.0 230
Aerospace CSci 451 3.0
Aerospace CSci 2.0 451

How does the reader know when to capture the element?

I'm so confused on this, any help would be really appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: I don't understand the concept of file reading

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: I don't understand the concept of file reading

Posted 28 September 2010 - 03:26 AM

As far as how to store the info from the lines in your program can I suggest you consider a struct.
Read here for more info
http://www.cplusplus...ial/structures/
Feel free to ask any questions you may have after reading it.

As far as this:

View Postbeefstew, on 28 September 2010 - 07:09 PM, said:

what if the file looks like this,
230 Aerospace CSci 3.0
Aerospace 230 CSci 4.0
Aerospace CSci 2.0 230
Aerospace CSci 451 3.0
Aerospace CSci 2.0 451

How does the reader know when to capture the element?

The 'reader' does not know.
The program is very very dumb unless you write code that teaches it how to be smarter.
If you really had to handle a case with scrambled data inputs like that you have got some interesting parsing and stuff to get working on. Not really hard in this case, but painstaking in ensuring you have thought through all the logic. logics.

First write some code that simple simply throws an error if the input is not in the order you expect. Once you have everything else working you can do some data cleansing error correction code. You may well need help with that stage of your code.

EDIT - Fix typos.

This post has been edited by janotte: 28 September 2010 - 03:59 AM

Was This Post Helpful? 0
  • +
  • -

#3 Enter_Kratos  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 72
  • Joined: 26-September 09

Re: I don't understand the concept of file reading

Posted 28 September 2010 - 05:57 AM

I agree with what Janotte said about using structs however this is only possible if the data you're trying to process originates from your own programs as you would have to rewrite it to output them accordingly.
If changing the output is not an option you might want to process the data line by line using getline() with the space as a delimeter in order to read the data into your array. Obviously this does require the data to be formatted correctly or it will fail.

Kratos

This post has been edited by Enter_Kratos: 28 September 2010 - 05:58 AM

Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: I don't understand the concept of file reading

Posted 28 September 2010 - 08:14 AM

View PostEnter_Kratos, on 28 September 2010 - 09:57 PM, said:

I agree with what Janotte said about using structs however this is only possible if the data you're trying to process originates from your own programs as you would have to rewrite it to output them accordingly.

Huh?
What are you trying to say here?
The line about " only possible if the data you're trying to process originates from your own programs" makes no sense as written. But perhaps you meant something other than what you wrote?

View PostEnter_Kratos, on 28 September 2010 - 09:57 PM, said:

If changing the output is not an option

Again, huh?

View PostEnter_Kratos, on 28 September 2010 - 09:57 PM, said:

you might want to process the data line by line using getline() with the space as a delimeter in order to read the data into your array.

This is something we completely agree on.
I am suggesting an array of structs.
You seem to have a problem with using an array of structs that I don't understand but apart from that detail we are in complete agreement.

View PostEnter_Kratos, on 28 September 2010 - 09:57 PM, said:

Obviously this does require the data to be formatted correctly or it will fail.

Unless of course we write some code to handle scrambled input.
Which is, after all, one of the key questions of the OP.
It will only fail if you let it fail because you found a case where recovery is not possible or because you simply choose to fail without attempting to recover.
Was This Post Helpful? 0
  • +
  • -

#5 Enter_Kratos  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 72
  • Joined: 26-September 09

Re: I don't understand the concept of file reading

Posted 29 September 2010 - 12:48 AM

Sorry Janotte I'm writing at work so unfortunately I don't always take as long to formulate my responses as well as I normally do.

Quote

only possible if the data you're trying to process originates from your own programs

Since the title of the thread was about understanding how to read a file I was trying explain that in order for him to read the data in as a struct he would have to output it to the file as one, that is of course if the file he's trying to read wasn't produced from another source. Obviously there's so many ways for the data to be read in so I was leaving it open at the end so that he could play about with different ways of parsing the data.

Sorry if I confused people and sorry again if I've only made things worse! Haha
Was This Post Helpful? 1
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: I don't understand the concept of file reading

Posted 29 September 2010 - 01:24 AM

View PostEnter_Kratos, on 29 September 2010 - 04:48 PM, said:

I was trying explain that in order for him to read the data in as a struct he would have to output it to the file as one


I think I see your point, which is good.

I would just put a rider on it along the lines of
... would have to output it to the file as one or convert the elements of the struct into a string before writing it out to the file (or some similar manipulation).

But, as I say, I think I now see what you were saying and it makes sense as a warning to the OP about making the right choices in their program design.
Thanks for the time in clarifying.

This post has been edited by janotte: 29 September 2010 - 01:35 AM

Was This Post Helpful? 0
  • +
  • -

#7 Enter_Kratos  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 72
  • Joined: 26-September 09

Re: I don't understand the concept of file reading

Posted 29 September 2010 - 05:12 AM

I'm glad we're both on the same page now and I hope the OP has a better understanding of the concept of reading and writing files.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1