newbie needs File I/O tips

This is a discussion on newbie needs File I/O tips within the C Programming forums, part of the General Programming Boards category; Just wondered if anyone could provide some hints on file I/O? It needs to save structures to a file if ...

  1. #1
    JohnY
    Guest

    Question newbie needs File I/O tips

    Just wondered if anyone could provide some hints on file I/O? It needs to save structures to a file if that helps

  2. #2
    Unregistered
    Guest

    sample of something I saw

    This is just a snippet of code.

    /*srcfile is the file pointer*/
    /*src_file is a filename passed by the user*/
    if ( (srcfile = fopen (src_file, "r") ) == (FILE *) NULL )
    {
    printf ("cannot open %s \n", src_file);
    exit (1); /*fopen returns NULL if problems so exit program*/
    }
    printf ("\n");

    for (j=0; j<40; j++) {
    /*reading in file into sturcture*/
    result = fscanf (srcfile, " %d", &rcd_shared->rcd_data [j].ch);
    if (result == EOF) break; /*break out when end of file */
    fscanf (srcfile, " %d", &rcd_data [j].rt);
    fscanf (srcfile, " %c", &rcd_data [j].tr);
    fscanf (srcfile, " %d", &rcd_data [j].sa);
    fscanf (srcfile, " %d", &rcd_data [j].wd);

    rcd_shared->rcd_Ctrl.number_of_signals = j + 1;
    }

    fclose (srcfile); /*must close file*/

    not the cleanest example but it gets the job done.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Made code of unregistered a bit more nice.

    Code:
    /* the return value is TRUE if everything went OK, else FALSE */
    bool return_value;
    
    /* pointer to the file */
    FILE *srcfile;
    
    /* name of the file */
    char *srcfile_name;
    
    /* try to open the file for read only */
    srcfile = fopen (srcfile_name, "r");
    
    if (srcfile != NULL)
    {
        /* file is opened */
        for (j = 0; (j < 40) && (result != EOF); j++) 
        {    
            /* reading in file into structure */     
            result = fscanf (srcfile, " %d", &rcd_shared->rcd_data [j].ch);         
            result &= fscanf (srcfile, " %d", &rcd_data [j].rt); 
            result &=fscanf (srcfile, " %c", &rcd_data [j].tr); 
            result &=fscanf (srcfile, " %d", &rcd_data [j].sa); 
            result &=fscanf (srcfile, " %d", &rcd_data [j].wd); 
            
            rcd_shared->rcd_Ctrl.number_of_signals = j + 1; 
        }
        
        /* close file */
        fclose (srcfile);
        
        return_value = TRUE;
    }
    else
    {
        printf ("Cannout open %s\n", srcfile_name);
        return_value = FALSE;
    }
    Avoid using the break-statement in a for-loop. Also avoid the use of the exit()-function. When needing these, it means that your program is bad-structured.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Subtle(?) File I/O Problem
    By cecomp64 in forum C Programming
    Replies: 9
    Last Post: 07-16-2008, 11:39 AM
  2. Question about file I/O from a newbie
    By henrik in forum C Programming
    Replies: 4
    Last Post: 11-12-2007, 11:48 PM
  3. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-17-2005, 11:09 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Newbie - file i/o - I'm stuck
    By djacko in forum C++ Programming
    Replies: 1
    Last Post: 02-22-2002, 08:56 AM

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