Thread: reading/writing to file

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    117

    reading/writing to file

    Ok so what I want to do it store an array of ints, in this case:
    Code:
    int a1[16] = {5,10,15,20,25,30,35,40,45,50,55,60,65,70,75}
    and later read them from file.


    So right now I produces the file "data.dat" which is just a .dat file containing "051015202530354045505560657075"

    Now I want to read from disk, but am having a problem. How do I read two spaces and store it into an int?

    Here is my code with write_File commented out (so that it just reads):

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void write_File(int* a1);
    void read_File (void);
    
    int main()
    {
    int i;
    int a1[16] = {5,10,15,20,25,30,35,40,45,50,55,60,65,70,75};
    
    //write_File(a1);
    
    read_File();
    return(0);
    }
    
    void write_File(int* a1)
    {
        int i;
    
        for(i=0; i<=14; i++)
        {
            FILE* fts;
    
            fts = fopen("data.dat", "a");
    
            if (fts)
            {
                fprintf(fts, "%d" , a1[i]);
                fclose(fts);
            }
    
    
            else
            {
                printf("Error writing to file!\n");
            }
    
        }
    
    }
    
    void read_File (void)
    {
    
        int i;
        int a2[16];
    
        for(i=0; i<=14; i++)
        {
    
            FILE* fts;
    
            fts = fopen("data.dat", "r");
    
            if (fts)
            {
                fread(a2, sizeof(int), 15, fts);
            }
    
    
            else
            {
                printf("Error reading to file!\n");
            }
    
    
            for(i=0;i<15;i++)
            {
                printf("%d\n", a2[i]);
            }
    
                fclose(fts);
    
        }
    
    }
    My Ctrl+S addiction gets in the way when using Code Blocks...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A simpler solution is to change write_File to write with fprintf(fts, "%d " , a1[i]); then you read with fscanf.

    EDIT:
    Also, I notice that you are repeatedly opening and closing the file within the loop. Don't do that. Open once, loop to read/write, then close.
    Last edited by laserlight; 01-24-2012 at 08:49 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    Ah this is so awesome! Thank you so much!! Really cut down the code for me and makes sense
    My Ctrl+S addiction gets in the way when using Code Blocks...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading and writing to a file
    By rocketman50 in forum C++ Programming
    Replies: 1
    Last Post: 06-15-2010, 12:36 PM
  2. reading and writing to a file
    By tikelele in forum C Programming
    Replies: 12
    Last Post: 11-26-2007, 11:37 PM
  3. Reading from and writing to same file
    By Strait in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2005, 04:37 PM
  4. reading & writing to a file
    By Micko in forum C++ Programming
    Replies: 5
    Last Post: 05-08-2004, 11:57 AM
  5. writing/reading txt file
    By Klinerr1 in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2004, 09:34 PM