Solved

problem with fgetc and getc and file reading

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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++.

691 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