Solved

problem with fgetc and getc and file reading

Posted on 1997-07-15
2
901 Views
Last Modified: 2008-03-17
I open a file with fopen, and fgetc and getc won't read the value of 0x0d from the file. It acts like it's not even there and moves on to the next value instead. For example, if there is the newline values( 0x0d, 0x0a) in the file, and I try to read the 0x0d, it will read the 0x0a instead of it.
0
Comment
Question by:mosfet
2 Comments
 
LVL 1

Accepted Solution

by:
8051 earned 200 total points
ID: 1165112
//Functions getc and fgetc are text-oriented.
//The best (and fastest) solution to open file as binary,
//use buffered reading (function fread) and handle this
//buffer as you want.

//Example following:

int format_to_one_line(char * input_name, char * output_name)
{
    if ((fin=fopen(input_name,"rb"))==NULL) return 2;
    if ((fout=fopen(output_name, "wb+"))==NULL) return 3;
    //------------------------------
    while (!feof(fin))
    {
        blocksize = fread(bufin,1,BUFSIZE,fin);
        j=0;
        if (blocksize)
        {
           for (i=0;i<blocksize;i++)
           {
               if (bufin[i]==0x0D)
               {
                  //delete CR/LF or LF/CR
                  if (bufin[i+2]!=' ' &&
                      bufin[i+2]!=0x0D &&
                      i<(blocksize-2) &&
                      bufin[i+2]!=0x09 && //TAB
                      !isdigit(bufin[i+2]))
                      {
                         i+=2; //Skip it
                         bufout[j++]=' ';
                      }

               }
               bufout[j++]=bufin[i];
           }
           if (fwrite(bufout,1,j,fout)<j) return 3;
        }
    }
    //------------------------------
    fclose(fin);
    fclose(fout);
    return 0;
}

0
 
LVL 1

Author Comment

by:mosfet
ID: 1165113
I appreciate your response.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question