Solved

problem with fgetc and getc and file reading

Posted on 1997-07-15
2
895 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now