File Input Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • a

    File Input Question

    (I've reached that familiar place where I've got a nagging little
    problem in a program I'm writing but I've been staring at code for too
    long and I probably wouldn't be able to recognize the answer even if I
    was staring right at it.)

    I'm trying to design a function that reads input from my data file into
    (a pair of) arrays. Simple enough? However, each line of the file is
    either going to be 4 integers or a character (then a carriage return)
    and I'm not sure which (i.e. there could be 4 integers followed by
    another line of 4 integers followed by a letter or it could just be 1
    letter/1 letter/1 letter). If the input is a character, it goes into
    charArray, but if the input are those 4 integers, then they take up the
    next 4 spots in intArray.

    So, basically, I need to know how to read in something from a file when
    I don't know its type. Also, I'm a bit rusty on the C++ and I've never
    done file I/O so I'm trying to do this in the simplest way possible but
    most of the file input examples I've seen online contain many function
    calls that I don't understand. So when explaining, please do the typing
    equivalent of speaking loudly and slowly =)





    //The initializations are simple enough:
    int in1, in2, in3, in4;
    char ch1;

    ifstream file_in;
    file_in.open("t est.txt");

    if (!file_in)
    {
    cout << "Cannot open the file test.txt" << endl;
    return (-1);
    }

    //And I think I have the while loop:
    while( (file_in >> in1 >> in2 >> in3 >> in4 ) || (file_in >> ch1) )

    //But then, I'm not sure how to check for type with my input


    thanks in advance,
    -frank (email: rocket34@NOSPAM .yahoo.com)

  • Attila Feher

    #2
    Re: File Input Question

    a wrote:[color=blue]
    > (I've reached that familiar place where I've got a nagging little
    > problem in a program I'm writing but I've been staring at code for too
    > long and I probably wouldn't be able to recognize the answer even if I
    > was staring right at it.)
    >
    > I'm trying to design a function that reads input from my data file
    > into (a pair of) arrays. Simple enough? However, each line of the
    > file is either going to be 4 integers or a character (then a carriage
    > return) and I'm not sure which (i.e. there could be 4 integers
    > followed by another line of 4 integers followed by a letter or it
    > could just be 1 letter/1 letter/1 letter). If the input is a
    > character, it goes into charArray, but if the input are those 4
    > integers, then they take up the next 4 spots in intArray.[/color]
    [SNIP]

    Since you input is line based, you will need to use the getline
    functionality. Get the line into an std::string and when it is there, you
    can find out if it is a 4*int or a 1*char. Be careful that a 1*char looks
    just like a 1*int.

    The iostream extract operator makes no difference between the any whitespace
    and the end of line, so your approach with the two reads hoping that the int
    reader will fail on a char is wrong. If the char happens to be 1, it will
    read it as the first integer. If there are 4 lines like this you will get 4
    intergers and so forth.

    If you did read a line into a string you can easily check if it has one
    character in it or not (by looking at the size). If not put this string
    into an istringstream and get the 4 integers out.

    --
    Attila aka WW


    Comment

    • a

      #3
      Re: File Input Question

      > Since you input is line based, you will need to use the getline[color=blue]
      > functionality. Get the line into an std::string and when it is there, you
      > can find out if it is a 4*int or a 1*char. Be careful that a 1*char looks
      > just like a 1*int.
      >
      > The iostream extract operator makes no difference between the any whitespace
      > and the end of line, so your approach with the two reads hoping that the int
      > reader will fail on a char is wrong. If the char happens to be 1, it will
      > read it as the first integer. If there are 4 lines like this you will get 4
      > intergers and so forth.
      >
      > If you did read a line into a string you can easily check if it has one
      > character in it or not (by looking at the size). If not put this string
      > into an istringstream and get the 4 integers out.[/color]

      Oops, I have a small problem. I reread what I wrote and I said exactly
      4 but it's actually anywhere from 1 to 4 integers on a line.
      Unfortunately, as you mentioned in the first paragraph, this creates a
      problem when I'm just trying to search on length alone. Is there any
      way to test for data type of the input rather than length of it?

      -frank

      -frank

      Comment

      • Attila Feher

        #4
        Re: File Input Question

        a wrote:[color=blue][color=green]
        >> Since you input is line based, you will need to use the getline
        >> functionality. Get the line into an std::string and when it is
        >> there, you can find out if it is a 4*int or a 1*char. Be careful
        >> that a 1*char looks just like a 1*int.
        >>
        >> The iostream extract operator makes no difference between the any
        >> whitespace and the end of line, so your approach with the two reads
        >> hoping that the int reader will fail on a char is wrong. If the
        >> char happens to be 1, it will read it as the first integer. If
        >> there are 4 lines like this you will get 4 intergers and so forth.
        >>
        >> If you did read a line into a string you can easily check if it has
        >> one character in it or not (by looking at the size). If not put
        >> this string into an istringstream and get the 4 integers out.[/color]
        >
        > Oops, I have a small problem. I reread what I wrote and I said
        > exactly 4 but it's actually anywhere from 1 to 4 integers on a line.
        > Unfortunately, as you mentioned in the first paragraph, this creates a
        > problem when I'm just trying to search on length alone. Is there any
        > way to test for data type of the input rather than length of it?[/color]

        Frank. Honestly. Who made that specification? Please let me know:

        1
        1
        1

        Was this 3 lines of one characters or 3 lines of one integer?

        --
        Attila aka WW


        Comment

        • a

          #5
          Re: File Input Question

          > Frank. Honestly. Who made that specification? Please let me know:[color=blue]
          >
          > 1
          > 1
          > 1
          >
          > Was this 3 lines of one characters or 3 lines of one integer?[/color]

          As for who made that design spec- it's poor data structure design. I'm
          in the process of refining it. Obviously it still has some work to go.
          *sheepish grin*

          Basically, the concept is that if a player (each designated by a letter)
          plays a variable number of games per week, hence the different number
          of, well, numbers. Basically, in the example below, player A (I'll get
          names implemented later- I'm just working on getting the smaller issues
          right first- but that string is another reason why I want to be able to
          determine things via data type) only had x number of games each week (2,
          1, 3 in the example below) and the numbers denote the number of points
          per game.

          A
          8 12
          1
          2 3 5


          As for
          1
          1
          1
          Those would all count as integers. Single integers are not characters-
          sorry for not clarifying.

          -frank

          Comment

          • Attila Feher

            #6
            Re: File Input Question

            a wrote:
            [NIP][color=blue]
            > Basically, the concept is that if a player (each designated by a
            > letter) plays a variable number of games per week, hence the
            > different number of, well, numbers. Basically, in the example below,
            > player A (I'll get names implemented later- I'm just working on
            > getting the smaller issues right first- but that string is another
            > reason why I want to be able to determine things via data type) only
            > had x number of games each week (2, 1, 3 in the example below) and
            > the numbers denote the number of points per game.
            >
            > A
            > 8 12
            > 1
            > 2 3 5[/color]

            OK. As I said: read lines into an std::string using getline, since your
            file is lone based. Then check the first character if it is a letter.
            Let's suppose you are only using that English 26, so it should be pretty
            simple. If it is a letter, you have it: a new player. If it is not, it is
            better be a number, because you put Mr. Std. String into a stringstream and
            read integers from it one by one in a loop, until you can.

            --
            Attila aka WW


            Comment

            • Mike Wahler

              #7
              Re: File Input Question


              "a" <a@a.net> wrote in message news:vn2ehnkph9 5ae1@corp.super news.com...[color=blue][color=green]
              > > Frank. Honestly. Who made that specification? Please let me know:
              > >
              > > 1
              > > 1
              > > 1
              > >
              > > Was this 3 lines of one characters or 3 lines of one integer?[/color]
              >
              > As for who made that design spec- it's poor data structure design. I'm
              > in the process of refining it. Obviously it still has some work to go.
              > *sheepish grin*
              >
              > Basically, the concept is that if a player (each designated by a letter)
              > plays a variable number of games per week, hence the different number
              > of, well, numbers. Basically, in the example below, player A (I'll get
              > names implemented later- I'm just working on getting the smaller issues
              > right first- but that string is another reason why I want to be able to
              > determine things via data type) only had x number of games each week (2,
              > 1, 3 in the example below) and the numbers denote the number of points
              > per game.
              >
              > A
              > 8 12
              > 1
              > 2 3 5[/color]

              Why not change the file format to tell you this information?
              E.g.

              A 3 // 3 is number of following lines related to 'A'
              2 8 12 // 2 is num of scores, 8 and 12 are the two scores
              1 1 // 1 is num of scores, 1 is the single score
              3 2 3 5 // 3 is num of scores, 2, 3, and 5 are the three scores.
              [color=blue]
              > As for
              > 1
              > 1
              > 1
              > Those would all count as integers. Single integers are not characters-[/color]

              Actually *everything* in a text file is a character,
              including single digit characters such as '1'.
              A sequence of digit characters can be converted
              to an integer type, but that doesn't stop them
              from being characters, i.e. valid as a string.
              [color=blue]
              > sorry for not clarifying.[/color]

              If you say that a single numerical character does not
              qualify as a 'character' in your file specification,
              then you'll have to disallow such from being a 'player
              name'.

              I think it's much more effective to create a good design
              for your file format, rather than trying to come up with
              'fancy tricks' to try to decipher one with a limited amount
              of information.

              -Mike


              Comment

              Working...