I'm developing in c++ but yet need to use FILE * from c (I know fstream option) I want to read line by line

string str;
char* line;
while(!feof(file)  && FIleLength(file)>0 && fgets(line, 1024, file))
{
    str=line;
    str.append("hi");
     fputs(str.c_str(),file)
}

I have a few concerns:
1) What if my line is over 1024 chars????????? how should I fix my code to handle that situstion
2)Do I need all these checks feof + size + fgets
3) if I want to use fgets but do not want to use char* line
what c++ type can I use and yet use fgets ()with any convertion probably

Recommended Answers

All 4 Replies

Try messing about with a skeleton like this:

#include <stdio.h>

void foo(FILE *in, FILE *out)
{
    char line[12]; /* pick a size, any size (greater than 1), but do pick a size */
    while ( fgets(line, sizeof line, [B]in[/B]) )
    {
        fputs(line, [B]out[/B]);
    }
}

int main()
{
    static const char filename[] = __FILE__;
    FILE *file = fopen(filename, "r");
    if ( file )
    {
        foo(file, stdout);
        fclose(file);
    }
    else
    {
        perror(filename);
    }
    return 0;
}

I prefer to check the return value of fgets rather than mess with eof .

I'm developing in c++ but yet need to use FILE * from c

Why?

1) What if my line is over 1024 chars????????? how should I fix my code to handle that situstion

If there is no '\n' at the end of the buffer you only got part of the line. This is one reason using string and getline() from C++ is better. Also in your code you never defined space for the input. All you did was define a pointer to who-knows-where and the first read will overwrite memory.

2)Do I need all these checks feof + size + fgets

No. Just check the return value from fgets() .

3) if I want to use fgets but do not want to use char* line what c++ type can I use and yet use fgets ()with any convertion probably

Nothing. fgets() only works with C-strings. At least not without unnecessary convolutions to use the wrong functions on mismatched variable types. It's like even though you have a perfectly good water glass next to you, you decide you want to take a drink by folding a piece of paper into a vessel.

I need to use FILE * ans not fstream because I want to lock the file and do not ewant to use handles (vs 2003 doesn't support this option with fstream open()
GetLine() would be great but it doesn't get FILE * so I cant use it.
I do not want to use char * lokking for any stringbuf or anything alike in c++ to use fgets()
(the code isn't the reall code which is too large for posting)

1) What if my line is over 1024 chars????????? how should I fix my code to handle that situstion

If you want to read the entire line at one time then just declare the input character array to be large enough to hold the largest possible line.

>>If there is no '\n' at the end of the buffer you only got part of the line.
Maybe yes, and maybe no. The '\n' is appended only if there is one in the file. Sometimes the last line of the file does not have '\n' at the end. So in that case the program would have to check the next read to see if fgets() reached end-of-file.


>>I do not want to use char * lokking for any stringbuf or anything alike in c++ to use fgets()

That has already been answered, and the answer is that it can not be done (easily). But what you could do is something like this:

char cline[2048];
std::string sline; 

while( fgets(cline, sizeof(cline), infp)
{
    char* ptr;
    if( (ptr = strchr(cline, '\n')) != NULL)
    {
          *ptr = 0; // truncate the '\n' character
          sline += cline;
          // do something with this line
    }
}

The code above would have to be a little more complex, but I think you get the idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.