Thread: C Programming question about file manipulation

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    7

    C Programming question about file manipulation

    Good evening!

    I have a C program that I have been working on for most of the week. I have gotten around 75% of it working. I'm stuck on the last part.

    The question:

    How do you read a file into memory, all into one buffer, reverse the contents of the buffer and then overwrite the buffer contents? I have a sample text file which I select in one of my menu options. I then use it in my option 4.

    I have as my 4th option:

    Code:
    else if (option == 4)
     
          { 
           fp = fopen(filename, "r");     //open file 
            
           //some other stuff inbetween 
            
           fclose(fp);                    //close file 
                
          }
    I believe I need to create and define the size of a buffer (max of 20,000 bytes), dump the contents of the chosen file into the buffer and then reverse contents. I close the file afterwards. If I choose to view the contents of the file I reversed (another option in my menu) I will see everything backwards. At least that is the plan.

    On top of that, if no file is selected an error is displayed.

    If anyone has a few suggestions, directions, examples or links to some really good tutorials on the subject of file manipulation I would greatly appreciate it.

    Eternal Thanks!

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    You use the fgets and fgetc function to read all the contents of file into buffer.You continue reading from file until reaching EOF in the file.For this,you can use feof function which will check whether file pointer reaches EOF or not.After you stored all the contents into buffer,you write your reverse function to reverse the content of the buffer.For this,you can use for loop or while loop to traverse the full content of the buffer to reverse.After that,open the same file in write mode and copy the content of buffer to file using fputs function.
    Last edited by vivekraj; 03-05-2010 at 09:54 PM.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    37
    For reading the file into the buffer you can use fgets and fgetc functions.

    Here is a sample program using fgets function.

    Code:
    #include <stdio.h>
    #include <string.h>
    main()
    {
            char s2[100];
            FILE *fp;
            fp = fopen("file","r"); //opening the file for reading. This file contains the data as this is for testing
    
            fgets(s2,sizeof(s2),fp);
            printf("s2 length  : %d\n",strlen(s2));
            printf("contents from file : %s\n",s2);
    }
    This will print as
    Code:
    This is for testing
    Last edited by thillai_selvan; 03-05-2010 at 09:58 PM.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    37
    If you want to overwrite the buffer with the reversed string you can use the following code
    Code:
    #include <stdio.h>
    #include <string.h>
    main()
    {
            char s2[100],temp;
            FILE *fp;
            fp = fopen("file","r");
            int i,len;
            fgets(s2,sizeof(s2),fp);
            printf("s2 length  : %d\n",strlen(s2));
            printf("contents from file : %s\n",s2);
            len=strlen(s2)-1;
            for(i=0;i<strlen(s2)/2;i++)
            {
                    temp =s2[i];
                    s2[i]= s2[len];
                    s2[len--]= temp;
            }
            printf("Reversed string in the buffer:%s\n",s2);
    }

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    Have this as an idea , the buffer contains all the file (example size 20000) ,
    Code:
    int main()
    {
            FILE *fp= fopen("filename", "r");     //open file
    
            char *buffer =malloc(20000); ;
            char buf[20000] ;
            int n = 0 ;
            n = fread(buffer, 20000,1, fp);
            int i=0;
            n = strlen(buffer);
            printf("buffer :%s == %d\n"  ,buffer,n);
    
                   char c ;
            for ( ; n>=0 ;n--)
            {
                    if ( (c=*(buffer+n)) !='\0'  )
                    {
                            buf[i++]= c ;
                    }
            }
    
            printf("reversed contents :%s\n" , buf);
               fclose(fp);                    //close file
    
    }
    Last edited by Alexander jack; 03-05-2010 at 10:27 PM. Reason: remove some lines

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    15

    Smile

    Use the following code
    Code:
    #include<stdio.h>
    #include<string.h>
    main()
    {
    void reverse(char[]);
    char s[30];
    int c;
    int i;
    FILE *fp;
    fp = fopen("file","r");
    while(!feof(fp))
    {
    fgets(s,sizeof(s),fp);
    reverse(s);
    }
    }
    
    void reverse(char s1[])
    {
    int c,i,j;
    for(i=0,j=strlen(s1)-1;i<j;i++,j--)
    {
    c=s1[i],s1[i]=s1[j],s1[j]=c;
    }
    printf("The reverse string is:%s\n",s1);
                                                  
    }

  7. #7
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64

    Thumbs up

    Code:
    #include <stdio.h>
    #include <string.h>
    #include<malloc.h>
    void reverse(char s[]);
    main()
    {
            char *buf=malloc(10000);
            FILE *fp;
            fp = fopen("test","r");
            int i=0;
            int c;
           while((c=fgetc(fp))!=EOF) # To read the entire file
           {
                  buf[i++]=c;
           }
           printf("File contents :\n %s",buf);
    
           printf("Reversed contents:\n");
           reverse(buf);
    }
    
    
    
    void reverse(char s[])
    {
            int c,i,j;
    
            for(i=0,j=strlen(s)-1;i<j;i++,j--)
            {
                    c=s[i];
            s[i]=s[j];
            s[j]=c;
            }
            printf("%s",s);
    }

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. you need to check that i does not exceeds buffer length
    2. before using any string manipulation functions on buffer - you need to nul-terminate it.

    for example
    Code:
    printf("File contents :\n %s",buf);
    will print the buffer till \0 char is encountered, which your buffer does not have...

    alternatively you can use
    Code:
    printf("File contents :\n %*.*s",i,i,buf);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    7

    C Programming question about file manipulation

    My thanks to everyone who posted a reply! All were excellent suggestions. I now have a better understanding of the process.

    This is what I went with:

    Code:
    else if (option == 4)
          {
           fp = fopen(filename, "r");     //open file
           if((fp == NULL))
           {
            printf("No file was selected!\n");
            exit(1);       
           }
                  
            char *buffer = malloc(20000);
            char buf[20000];
            
            n = fread(buffer, 20000, 1, fp);
            n = strlen(buffer);
            printf("buffer :%s == %d\n"  ,buffer,n);
    
            for ( ; n>=0 ;n--)
            {
             if ( (c=*(buffer+n)) !='\0'  )
             {
              buf[i++]= c ;
             }
            }
            printf("reversed contents :%s\n" , buf);
                  
           fclose(fp);                    //close file
          }
    The program now runs! After a file is selected and Option 4 is chosen, the screen will say "buffer :" and display the contents of the text file. Right below it, it says "reversed contents :" and then displays the text file's contents, but backwards.

    Now, what I'm trying to do is take the reversed contents in the buffer and overwrite the text file. In other words, replace the contents of the selected text file with the reversed content. If you were to open the text file, everything would be backwards.

    So, I believe I need to write the reversed buffer contents to the file before fclose(fp);? that way, when I open the text file again, I'll see everything backwards.

    In theory, anyway... ;0)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM