Using 'For' with fprintf/fscanf

This is a discussion on Using 'For' with fprintf/fscanf within the C++ Programming forums, part of the General Programming Boards category; Hello. My question is the following, I want to save in a file / read from a file, an entire ...

  1. #1
    Registered User Pecado's Avatar
    Join Date
    Sep 2010
    Posts
    34

    Using 'For' with fprintf/fscanf

    Hello.
    My question is the following, I want to save in a file / read from a file, an entire vector with different values in each position. But, if it is a very long vector... instead of doing something like this:

    Code:
    fp = fopen ("Datos.txt","w");
    fprintf (fp, "%d %d %d %d %d ", vector[1], vector[2], vector[3], vector[4], vector[5]);
    fclose (fp);
    But if we're talking about a really long vector, it can become tedious.
    Maybe there could be done something like:

    Code:
    for(f=0;f<6;f++)
    {fprintf(fp,"%d",vector[f]);
    I know it's wrong, it's to give a general idea. Any help is apreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    Is there any reason you chose to use C-style I/O over more C++ style I/O?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    How is it wrong?

    Pedantically, your for loop prints out 6 values rather than 5

    It also does not include a space between the numbers, but you could easily fix that by doing "%d " rather than "%d"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User Pecado's Avatar
    Join Date
    Sep 2010
    Posts
    34
    Quote Originally Posted by laserlight View Post
    Is there any reason you chose to use C-style I/O over more C++ style I/O?
    Yes, currently, lack of time to study c++. (Just realised I posted it on C++ forum, sorry for that.)
    But is it really better or simpler in C++? I mean, C one is pretty much simple.

    Quote Originally Posted by Salem View Post
    How is it wrong?

    Pedantically, your for loop prints out 6 values rather than 5

    It also does not include a space between the numbers, but you could easily fix that by doing "%d " rather than "%d"
    Well apparently I was typing something in a wrong way because I re-checked and it does work.

    I appreciate the help guys. Thanks.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,677
    Since this was posted in a C++ forum it is worth noting that, in C++, the solution is arguably simpler than in C (assuming included appropriate headers). For example, to output the first five elements of an int array named myarray to an ostream named out, with a space after each element .....

    Code:
       std::copy(myarray, myarray + 5, std::ostream_iterator<int>(out, " "));
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Tags for this Thread


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