Thread: files and streams

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Question files and streams

    Greetings,

    Assuming that inStream is opened as a connection to an input file that contains the following data:

    b = blank

    below is the data: in inStream

    b 54 b 32E1 enter -6.78 b $80 enter enter b 1 enter eof()

    and the following declarations are

    int n1, n2, n3, n4;
    double
    r1, r2, r3;
    char
    c1, c2, c3, c4

    List the values that are assigned to each of the variables in the input list or explain why an error occurs.

    eg;
    inStream >> n1 >> n2 >> c1 >> c2 >> c3
    >> r1 >> c4 >> n2 >> r2;

    I understand that inStream uses <fstream> and basically its like cin in <istream> . Am i correct to say that the >> reads a variable and skips past the newlinw, blanks, etc.... Can anyone give me an example.

    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Please clarify your request. It seems as if you are providing an example yourself.

    Yes, you are correct in saying that applying the extraction operator (>>) to an object of type ifstream (which is a subclass of istream, and as such is similar in form to cin, as well as objects of type istringstream) will extract the next available data from that ifstream, skipping when it hits a space or newline.

  3. #3
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Hello Imperito!

    Sorry for the confusion.
    Can anyone begin by given me answers to the first 3 or 4 variables e.g; n1 = ?? c1 = ?? r1 = ?? This has got me confused although i understand n1 is int, c1 is a char '#' and r1 is a double value. I am confused on what the output should look for these, plz help?

    b 54 b 32E1 enter -6.78 b $80 enter enter b 1 enter eof()

    and the following declarations are

    int n1, n2, n3, n4;
    double
    r1, r2, r3;
    char
    c1, c2, c3, c4

    List the values that are assigned to each of the variables in the input list or explain why an error occurs.

    eg;
    inStream >> n1 >> n2 >> c1 >> c2 >> c3
    >> r1 >> c4 >> n2 >> r2;
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  4. #4
    Evil Member
    Join Date
    Jan 2002
    Posts
    638

    Have you considered...

    Generating the file specified, creating a program to read frm thwe file, and assessing the output yourself?

    that would tell you more about the nature of what you are trying to do than I could.

    You have the program almost entirely worked out, just open an ifstream called inStream and then read the variables using that line of code you showed, then just output them back to cout to see that they are.

  5. #5
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    hello imperito!!!

    I have considered it. I wrote my program and it compiles without errors, seems to work but when i tried it with another problem in the book my answers didnt match, i was hoping anyone could quickly come up with those answers to make sure i ws doing it right.
    Thanks anyway for you help and for responding quickly to my aid.
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  6. #6
    Evil Member
    Join Date
    Jan 2002
    Posts
    638

    Well, why not take a look at the file...

    Code:
     54 32E1
    -6.78 $80
    
     1
    First of all, do you agree that that is what the file should look like?

    If so, lets move on to the extractions...

    The first thing it tries to extract is n1. This is an integer, so it looks for the first thing in the stream that it can recognize as an integer. The number 54 in this case.

    Then it tries to extract n2, another integer. The top of the stream is 32E1, but only the 32 part make an integer. So it extracts 32, and stores it.

    Then you ask for c1, a char. So it extracts the first char it finds, which is 'E'.

    Then you ask for another char, so it looks, and finds '1'.

    Then you ask for ANOTHER char. It looks, and finds '-'.

    Then you ask for a double. It has 6.78, so it takes that.

    Are you getting the pattern? If this isn't clear enough, try to explain just what is getting you confused...

  7. #7
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Thanks imperito!

    I understand it better now but for some reason couldn't understand why I couln't understand it in the first place.
    Thanks amigo!

    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. files & streams
    By gunghomiller in forum C++ Programming
    Replies: 17
    Last Post: 08-04-2007, 10:19 PM
  2. streams and files
    By chad101 in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2005, 11:54 AM
  3. Need help with input streams from multiple source files
    By orikon in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2005, 02:56 PM
  4. files
    By Raven Arkadon in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2005, 02:18 PM
  5. Dealing with i/o streams in a class
    By ender in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2002, 05:26 PM