?
Solved

problem with fgetc and getc and file reading

Posted on 1997-07-15
2
Medium Priority
?
910 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 1

Accepted Solution

by:
8051 earned 800 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

764 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