Thread: Changing characters in a file using a buffer

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    30

    Angry Changing characters in a file using a buffer

    Hi. I did this simple program to change the '<' characters to '>' in a file and vice-versa. However, when writing the new file the newlines got screwed up and there seems to be random spaces. It was compiled using Dev-cpp 4.9.2.

    What is wrong? Should I instead read the file char by char using fgetc and do the changes directly instead using the char* buffer?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    unsigned char* buf;
    
    int main(int argc, char *argv[]) 
    {
        
        if (argc != 2)
        {
          printf("Usage: fix_ch file\n");
               
          return -1;
        }
        
        char ch;
        int size, status, i = 0;
        
        FILE *fp = fopen(argv[1], "r+b");
        
        if (fp == NULL)
        {
          printf ("Error opening file.\n");
          return -1;      
        }
    
        fseek (fp, 0 , SEEK_END);
        size = ftell (fp);
        rewind(fp);
        
        buf = (char*)malloc(sizeof(char)*size);
        fread(buf, 1, size,  fp);
        
        fclose(fp);
          
          for (i = 0; i < size; i++)
          {     
            if (buf[i] == '>')
            {
              buf[i] = '<';
              status = 1;
            }
          
            if (buf[i] == '<' && status != 1)
            {
              buf[i] = '>';
            }
            status = 0;
         
          }
          
        fp = fopen(argv[1], "w");      
        
          for (i = 0; i < size; i++)
          {     
            fputc(buf[i], fp);      
          }    
          
                
      	return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Just one comment, I would simply to this...it's more readable:
    Code:
    for (i = 0; i < size; i++)
    {    
        switch (buf[i])
        {
            case '>':
                buf[i] = '<';
                break;
            case '<':
                buf[i] = '>';
                break;
            default:
                break;
        }
    }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You might have a file mode problem with the write out. Is "w" on your compiler, a text or a binary mode.

    If you read data in using binary mode, but write it back out using text mode, aren't you going to be goofed, since the binary data didn't get "translated" into text mode, by your compiler, before you made these changes?

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    30
    Ops, a few minutes after I posted I realized I should have used the switch statement :P.

    Anyway, the problem was really the file mode, specyfing "wb" corrected the problem. Thanks a lot, I wouldn't have discovered this by myself.

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. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM