Results 1 to 8 of 8

Thread: C++ read/write to files     submit to reddit submit to twitter

  1. #1
    United States of Smash!
    Join Date
    Jun 2006
    Posts
    8,810
    BG Level
    8

    C++ read/write to files

    I am taking beginning C++ class for work and I am having major issues on my lab right now. We need to read numbers from an input file and write every nth number from that input file to an output file.

    For some reason I am reading just the last quarter of the file only and it is not skipping any of the numbers.

    Code:
        while ( inF.good() )
            {
               if ( (indexNumber % (skipNum + 1)) == 0 )
                  {
                      inF >> currentNumber;
                      cout << currentNumber << endl;
                  }
                  indexNumber++;
            }
    So I made an indexNumber that starts at 1 and increments each time the while statement runs. I check to see if it is divisible by the skipNum which is the amount of numbers needed to skip and then if it is divisible by that number I grab the number and read it and then output it to consol. I am skipping the output portion to the new file for now until I get this part working.

    Anyone have any ideas of what I am doing wrong?

    Edit: Oh yeah and if I do a cout << indexNumber << endl;

    it prints incredibly large numbers and starts at 15628 even though it is initialized at 1 at the beginning. Is it working and the consol window just can only display so many lines before they disappear?

    Edit2: Another thing when I print out the indexNumber it prints out 5 times for every time the input number prints. ... hmm there seems to be a clue there.

  2. #2
    Banned.

    Join Date
    Oct 2006
    Posts
    10,115
    BG Level
    9

    why do you add 1 to the skipnum?

    inF >> currentNumber; increases the file pointer? I've never done file I/O with C++

  3. #3
    United States of Smash!
    Join Date
    Jun 2006
    Posts
    8,810
    BG Level
    8

    Each time inF reaches a space in the file it moves to the next set of characters automatically. I add 1 to skipNum because I want the number after the ones skipped. So if I skip 5 I want the 6th number. The user inputs 5 numbers to skip that means I want the 6th number so I take skipNum add one to it to get the number that I want. And if I want the 6th number then I do indexNum % 6 (or skipNum +1)

    Then if that evaluates to 0 I know my index number is divisible by 6 and I grab that number. Or at least that was the original idea. For some reason it isn't working.

  4. #4
    Warrior Tank
    Join Date
    Nov 2007
    Posts
    607
    BG Level
    5
    WoW Realm
    Burning Blade

    Have you initialized indexNumber = 0 ?

  5. #5
    United States of Smash!
    Join Date
    Jun 2006
    Posts
    8,810
    BG Level
    8

    Never mind on the output I figured out my issue. I was using an IF statement I switched it and turned it into a for loop and now it works I think. I will know when I get to the other part about transforming the numbers to characters.

    And yes indexNumber is initialized.

  6. #6
    Warrior Tank
    Join Date
    Nov 2007
    Posts
    607
    BG Level
    5
    WoW Realm
    Burning Blade

    Yeah I would always use a for loop if there is value you want to increment each cycle. It's very standard and makes it easier to debug because all the data associated with the loop is in one place.

  7. #7
    Banned.

    Join Date
    Oct 2006
    Posts
    10,115
    BG Level
    9

    Edit: nvm I should get to sleep -.-

  8. #8
    United States of Smash!
    Join Date
    Jun 2006
    Posts
    8,810
    BG Level
    8

    No .good() does return false at the end of the file. I took out the if statement and now it closes properly and stops writing at the end of the file.

    Now I have to convert the numbers to characters and do a carriage return after 10 spaces. I already have it outputting the characters and I decoded the message.

Similar Threads

  1. Replies: 8
    Last Post: 2009-01-11, 16:09
  2. Converting video files to .WMV for the 360
    By Lockecole in forum Tech
    Replies: 6
    Last Post: 2007-10-12, 17:46
  3. Replies: 3
    Last Post: 2007-07-23, 09:16