help me with files

This is a discussion on help me with files within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by xclarkiex5x infile is a file stream so is outfile... i think Right, so what I said is ...

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by xclarkiex5x View Post
    infile is a file stream so is outfile... i think
    Right, so what I said is that "infile >> something" also returns a value that tells you if it was sucessfull or not. So how would you use that with your work?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    ok and you were correct with your sum of the numbers instead of my count of the numbers
    which means ill probably have to change the program

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by xclarkiex5x View Post
    ok and you were correct with your sum of the numbers instead of my count of the numbers
    which means ill probably have to change the program
    You'll have to change it a tiny bit - most of it is the same whether you count something or sum something up, right? You still read numbers from a file and output to a file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    so ill need more variables maybe?
    and have a sum somewhere.

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Obviously, you will need some variable to hold your sum - and calling it sum would be a good idea, because that gives people some idea of what it's used for. On the other hand, you no longer need a count variable [although it could be useful to CHECK how many numbers you've added together, to see if you believe that your program does the right thing].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    youll be happy to know that i got the count program working without a hitch.. i used
    Code:
    (!infile.eof())
    for the while statement. now just to modify that to make it work for the sum of numbers instead of the count of the numbers

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That is actually incorrect - infile.eof() will only tell you that you are at the end of file when you have attempted to read PAST the end of file, so you will do one more loop than you exect to do.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    hmm it gives me the correct amount of numbers in the file whenever i change the input file for the count program

  9. #24
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    what would you suggest instead of infile.eof()?

  10. #25
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by xclarkiex5x View Post
    what would you suggest instead of infile.eof()?
    What I already said:
    Code:
    infile >> somethign
    will give a "return value" based on the success of the read operation. I guess I have to spoon-feed you:
    Code:
    while(infile >> number) ...
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #26
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    ok yah programing is somewhat new to me so its gonna take me a while to get some of the things your trying to say

  12. #27
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    so now onto the part with adding the numbers together.... i know i have to use infile >> variable... but how to order it from there confuses me...

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So give it your best shot. Have you not written any code before to add things together, that you could use as a "template"?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ressources files
    By mikahell in forum Windows Programming
    Replies: 4
    Last Post: 06-19-2006, 07:50 AM
  2. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 04:28 AM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 05:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21