Problem with fseek(), fprintf(), fscan()

This is a discussion on Problem with fseek(), fprintf(), fscan() within the C Programming forums, part of the General Programming Boards category; Hi everyone. I'm making a program that uses the functions fseek(), fprintf() and fscanf(). It doesn't work when I recover ...

  1. #1
    Unregistered
    Guest

    Question Problem with fseek(), fprintf(), fscan()

    Hi everyone.

    I'm making a program that uses the functions fseek(), fprintf() and fscanf(). It doesn't work when I recover the data I just stored. here's the code:

    #include <stdio.h>
    #include <stdlib.h>

    void WriteData(), ReadData();
    FILE *fp;
    int Data[10];
    long int i;

    int main (void) {

    WriteData();
    ReadData();
    }

    void WriteData() {
    /* I open the file here with fp = fopen()... no problem */
    /* I don't use register 0, so I put a cero in it */
    fseek(fp, 0, SEEK_SET); /* locate in register cero */
    fprintf(fp, "%d", 0); /* write a cero */

    /* here I fill the array Data[] with int values, no problem */
    .
    .
    .
    /* Write the data to the file starting with register one, and end with number ten */

    for (i=1; i<10; i++) {
    fseek(fp, i, SEEK_SET); /* point to register i in the file */
    fprintf(fp, "%d", Data[i]); /* write the data */

    }
    fclose(fp);
    }

    void ReadData() {
    int D;

    /* here I open the file as same as above, and get fp */




    /* here I read the data as follows */
    for (i=1; i <10; i++) {
    fseek(fp, i, SEEK_SET); /* Points to the register i in the file */
    fscanf(fp, "%d", &D); /* reads the data */
    Data[i] = D;
    }
    }


    End of story.....

    When I output to the screen the array Data[], it is really a mess, because i get negative and very big numbers.

    Would you help me on this? anyone?

    Thanks in advance.

    Gustaff.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, since you haven't actually shown us all of your code, we can just assume you're doing everything right. If you were, you wouldn't be posting, so we have to assume that the code you haven't provided contains the error.

    How about you actually post all of your code? Additionally, use [c0de] [/c0de] tags. (Replace the zero with an o.)

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems writing some chars to files with fprintf
    By Nazgulled in forum C Programming
    Replies: 3
    Last Post: 04-18-2006, 07:00 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 04:19 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 02:04 AM

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