Solved

problem with fgetc and getc and file reading

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 134
Interoperability issue between VC++ (ocx) & VB.NET applications 7 74
Header of docx file 17 97
Error C2678: binary '!=': no operator found... 4 41
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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.

919 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

21 Experts available now in Live!

Get 1:1 Help Now