fgets

leak in fgets when assigning to buffer

I'm having problems understanding why following code leaks in one case, and not in the other case. The difference is while(NULL!=fgets(buffer,length,file))//doesnt leak while(NULL!=(buffer=fgets(buffer,length,file))//leaks I thought it would be the same. Full code below. #include <stdio.h> #include <stdlib.h> #define LENS 10000 vo...

fgets equilvilant in C++

what is the C++ equivilant to the c function fgets? I have looked at getline from ifstream, but when it comes to a end of line character, '\n', it terminates at and discards it, I am looking for a function that just terminates at the end line character but adds the end of line character to the char array. thanks in advance ...

How to use fgets if you don't know the number of characters to be read?

I need to read a file and send the text from it to a string so I can parse it. However, the program won't know exactly how long the file is, so what would I do if I wanted to use fgets, or is there a better alternative? Note: char *fgets(char *str, size_t num, FILE *stream); ...

fgets instructions gets skipped.Why?

Whenever I do a scanf before a fgets the fgets instruction gets skipped. I have come accross this issue in C++ and I remember I had to had some instrcution that would clear the stdin buffer or something like that. I suppose there's an equivalent for C. What is it? Thanks. ...

In Perl, can I limit the length of a line as I read it in from a file (like fgets)?

I'm trying to write a piece of code that reads a file line by line and stores each line, up to a certain amount of input data. I want to guard against the end-user being evil and putting something like a gig of data on one line in addition to guarding against sucking in an abnormally large file. Doing $str = <FILE> will still read in a...

reading in a file and getting the string length

Hello, gcc 4.4.4 c89 I am using the following code to read in file using fgets. I just want to get the gender which could be either M or F. However, as the gender is always the last character in the string. I thought I could get the character by using strlen. However, for some reason I have to get the strlen and minus 2. I know that t...

gets not taking the password

In the code below in lesson2() i have used a password to enter the function but when i enter the function it does not takes in the passord and says incorrect password.By not taking in the password,i mean to say that i have used gets but its waiting for me to input a password.please dont tell me not to use gets! #include<stdio.h> #includ...

Php Buffer Problem fgets (Bandwidth)

Hello i'm saxtor, i'm creating this download quota program, so what i am doing is fetching the file from a remote server and buffering it out back to the client however i would like to record the buffer output to the client and record how much bytes the client has downlaoad, what the code supposed to do is to limit the client if he or s...

PHP script gets progressively slower (file reader)

I have a script that, when put against a timer, gets progressively slower. It's fairly simple as all it does is reads a line, checks it then adds it to the database, then proceeds to the next line. Here's the output of it gradually getting worse: Record: #1,001 Memory: 1,355,360kb taking 1.84s Record: #1,001 Memory: 1,355,360kb taking...

Automatic space after fgets' result?

Hi there, it seems as if fgets puts a space after everything it returns. Here's some example code: <?php Echo "Opening " . $_SERVER{'DOCUMENT_ROOT'} . "/file.txt" . "...<br>"; $FileHandle = @Fopen($_SERVER{'DOCUMENT_ROOT'} . "/file.txt", "r"); If ($FileHandle){ Echo "File opened:<br><br>"; While (!Feof($FileHandle)){ $L...

Sensible line buffer size in C?

I'm using popen to read output from shell commands. I will use fgets to read line by line. My question is how to choose the best buffer size for my char* buffer? I remember from a professor telling us to include <limits.h> and use LINE_MAX for such things. It works fine on my Mac, but there's no LINE_MAX on Linux. This mailing list arch...

Add string to char array in C

I have a C array called buf. Here is it's definition: char buf[1024]; Now, my current code takes from stdin and uses fgets() to set that array, however I wish to use code to set it instead. Right now the line that sets buf looks like this: fgets(buf, 1024, stdin); Basically, I want to replace stdin, with say... "My String". What's th...

fseek() by line, not bytes?

I have a script that parses large files line by line. When it encounters an error that it can't handle, it stops, notifying us of the last line parsed. Is this really the best / only way to seek to a specific line in a file? (fseek() is not usable in my case.) <?php for ($i = 0; $i < 100000; $i++) fgets($fp); // just discard this ...

Strange fgets() problem

I'm trying to run this code on Windows 7 (64-bit, if it matters) after compiling it with GCC. If I declare bufsize as an int, the program freezes and Windows informs me that it's stopped working. If I use #define bufsize 123 it works fine, and it works fine if I replace bufsize with a number myself. What am I missing here? int mai...

Reading in and storing using fgets()

I'm using fgets to read in text from simple files such as txt files however I need the ability to jump back to previous lines. Is there anyway to do this using fgets? Or should I just store the text in some sort a array/structure? ...

How do I read white space using scanf in c?

...

Easiest way to read this line of text into a struct?

I have a text file with data in the form: Lee AUS 2 103 2 62 TRUE Check AUS 4 48 0 23 FALSE Mills AUS 8 236 0 69 FALSE I need to each line into a struct like, however I'd like to avoid using fixed length arrays (the problem with fgets as far as I can tell): struct Data { char *sname; char *country; int *a; int *b; ...

Reading a sentence from stdin using fgets

I've written the following code to read a line from a terminal window, the problem is the code gets stuck in an infinite loop. The line/sentence is of undefined length, therefore I plan to read it in parts into the buffer, then concatenate it to another string which can be extended via realloc accordingly. Please can somebody spot my mi...

C fgets question

struct DVDInfo *ReadStruct( void ) { struct DVDInfo *infoPtr; int num; char line[ kMaxLineLength ]; char *result; infoPtr = malloc( sizeof( struct DVDInfo ) ); if ( NULL == infoPtr ) { printf( "Out of memory!!! Goodbye!\n" ); exit( 0 ); } printf( "Ent...

fgets naturally puts a terminating zero in C?

struct DVDInfo *ReadStruct( void ) { struct DVDInfo *infoPtr; int num; char line[ kMaxLineLength ]; char *result; infoPtr = malloc( sizeof( struct DVDInfo ) ); if ( NULL == infoPtr ) { printf( "Out of memory!!! Goodbye!\n" ); exit( 0 ); } printf( "Ent...