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

  1. #1

    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) {


    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 */


    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.


  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    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.)

    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, 06: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, 01:04 AM