fgets problems newline


 
Thread Tools Search this Thread
Top Forums Programming fgets problems newline
Prev   Next
# 1  
Old 11-09-2010
MySQL fgets problems newline

hello,
i'm trying to write a C-program that reads a file line by line.
(and searches each line for a given string)

This file is an special ASCII-database-file, with a lot of entries.
I checked the line with most length, and it was about 4000 characters.

With google i found several pages that said, fgets would get the length of chars given or until the newline character '\n' was found.
Because it didn't work, my first approach was to change the format of the file with the programs dos2unix and unix2dos.
Unfortunately my Code still doesn't work.

I also tried changing my LINE macro (max line length) to all possible values and I don't know what to do anymore. Please Help! Smilie

Code:
 

/* lib includes */
/**************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>

/* macro definitions */
/**************************************************************************************************/
#define LINE 4096

/* searchFileLines */
/**************************************************************************************************/
int searchFileLines( const char *filename, const char *searchstring ) {

    FILE *pFile;
    char buffer[LINE];
    int it = 0;

    if( ( pFile = fopen( filename, "r" ) ) == NULL ) {
        fprintf( stderr, "error: could not open file: %s\n", filename );
        return 1;
    }

    while( fgets( buffer, sizeof( buffer ), pFile ) ) {

        it++;
        if( strchr( buffer, '\n' ) == NULL ) {
            fprintf( stderr, "input line too long.\n" );
            //exit( 1 );
        }
        
        printf( "%d: %s\n", it, buffer );
    }

    return 0;
}

/**************************************************************************************************/
int main( const int argc, const char *argv[] ) { 

    /* 1. check filepath */
    /**************************************************************************/
    if( argc != 2 ) {
       printf( "dude, you need to give me a filepath!" );
       return( 1 );
    }

    /* 2. searh File */
    /**************************************************************************/
    if( !searchFileLines( argv[0], "NxNSignOff_ChangeStatusComment" ) )
        fprintf( stderr, "error: could not search file: %s\n", argv[0] );

    return 0;
}

What I mean by it doesn't work:
Allthough the File has about 1400 entries, my code just loops like 23 times and the output given is not readable text that is in the file.

Last edited by jim mcnamara; 11-09-2010 at 07:44 AM.. Reason: php -> code tags
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Newline in the file

I have requirement to remove the /n ( newline ) characters from the file. When I open file in VI .. I want to see newline char how to display newline char .. or where can I see the content with newline char visible? (3 Replies)
Discussion started by: freakabhi
3 Replies

2. Shell Programming and Scripting

echo without newline

I am trying to make a download progress meter with bash and I need to echo a percentage without making a newline and without concatenating to the last output line. The output should replace the last output line in the terminal. This is something you see when wget or curl downloads files.... (6 Replies)
Discussion started by: locoroco
6 Replies

3. Programming

fgets read file line with "\n" inside

Hi, I have a string like this, char str ="This, a sample string.\\nThis is the second line, \\n \\n, we will have one blank line"; if I want to use strtok() to seperate the string, which token should I use? I tried "\n", "\\n", either not working. peter (1 Reply)
Discussion started by: laopi
1 Replies

4. Programming

fgets problems

I've been having trouble with reading past the end-of-file in C. Can anyone find my stupid mistake? This is the minimal code needed to cause the error for me: FILE *f = fopen(name, "r"); if (!f) return; pari_sp ltop = avma; char line; while(fgets(line, 1100, f) != NULL) printf(".");... (23 Replies)
Discussion started by: CRGreathouse
23 Replies

5. UNIX for Dummies Questions & Answers

I'm having problems with a simple for loop on a newline

for i in `seq 1 10 ` ; do printf $i '\n'; done gives me this: 1234567891064mbarch ~ $ (output followed by bash prompt) :( I've tried so many ways to create a newline at the end. Does anyone have any ideas.. Thanks in advance. Sorry (7 Replies)
Discussion started by: 64mb
7 Replies

6. Programming

[C] fgets problem with SIGINT singlal!!!

Hi all, I have this method to read a string from a STDIN: void readLine(char* inputBuffer){ fgets (inputBuffer, MAX_LINE, stdin); fflush(stdin); /* remove '\n' char from string */ if(strlen(inputBuffer) != 0) inputBuffer = '\0'; } All work fine but if i... (1 Reply)
Discussion started by: hurricane86
1 Replies

7. Programming

Question about NULL Character & fgets()

Assume client send the message " Hello ", i get output such as Sent mesg: hello Bytes Sent to Client: 6 bytes_received = recv(clientSockD, data, MAX_DATA, 0); if(bytes_received) { send(clientSockD, data, bytes_received, 0); data = '\0';... (2 Replies)
Discussion started by: f.ben.isaac
2 Replies

8. Forum Support Area for Unregistered Users & Account Problems

newline

I have an old file originally created in vi but read and saved by a word processor at some point. I have ^Ms and know how to substitute them for anything I wish but I still only have one long line when viewed in vi. So I suppose I need to substitute a newline for each ^M but I don't know the... (2 Replies)
Discussion started by: Gale Gorman
2 Replies

9. Programming

Problem with fgets and rewind function ..

Hello Friends, I got stuck with fgets () & rewind() function .. Please need help.. Actually I am doing a like, The function should read lines from a txt file until the function is called.. If the data from the txt file ends then it goes to the top and then again when the function is called... (1 Reply)
Discussion started by: user_prady
1 Replies

10. Programming

fgets()

does anyone knows how to accept a command from a user.. i was wondering to use fgets(), but got no idea how to start it... (4 Replies)
Discussion started by: skanky
4 Replies
Login or Register to Ask a Question