fgetc

This is a discussion on fgetc within the C Programming forums, part of the General Programming Boards category; I am using fgetc to read a file and then take and input a space after each character read. That ...

  1. #1
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926

    fgetc

    I am using fgetc to read a file and then take and input a space after each character read. That part works fine, but I want to get the \n to input into my array. For some reason it doesn't get it. Does fgetc read a \n
    Code:
    while((c=fgetc(ffile))!=EOF){
                    if(c=='\n'){ /*this doesn't work*/
                            buffer[0]='\n';
                    }
                    else{
                            buffer[0]=c;
                            buffer[1]=' ';
                            buffer[2]='\0';
                    }
    btw buffer is declared as char buffer[3]; thanx

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    /* The test file...
    This is a test.
    This is a test of the Emergency C Board System.
    This is only a test.
    
    First and last comment line are not in the file.*/
    
    #include <stdio.h>
    
    int main( void )
    {
            FILE *fp = fopen( "testo.txt", "r" );
            int c;
    
            if( fp == NULL )
            {
                    printf("Error opening file.\n");
                    exit( 0 );
            }
            while( (c = fgetc( fp )) != EOF )
            {
                    if( c == '\n' )
                    {
                            printf("Works for me.\n");
                    }
            }
            fclose( fp );
            return 0;
    
    }
    
    /*The output...
    Works for me.
    Works for me.
    Works for me.
    Works for me.
    ...Four lines, because there are three lines plus one blank line in the test file. */
    
    Like it says, it works for me. Put a printf statement in the middle of your assignment loop to see if it is in fact reading the newline.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    it works just something wrong with somewhere else in my code. I'll figure it thanx

  4. #4
    Registered User scrapedbr's Avatar
    Join Date
    May 2003
    Posts
    19
    the code you have sent its ok.

    Code:
    while((c=fgetc(fp))!=EOF){
    			if(c=='\n'){ /*this doesn't work*/
    				buffer[0]='\n';
    				
    			}
    			else{
    				buffer[0]=c;
    				buffer[1]=' ';
    				buffer[2]='\0';
    			}
    	}
    i see the problem when ovewrite the [0] position in your buffer.

    if you put a printf with integer, you will se the value 10 (correponding ascii value. Take a look at www.asciitable.com .
    Code:
    if(c=='\n'){ /*this doesn't work*/
    				buffer[0]='\n';
    				printf("\nCaracter = %d\n",buffer[0]);				
    			}

    printk("Links: the only masculine choice for console-based browsers on Unix"), by Alpharoid.
    cscience.org
    gobolinux.org
    Gobolinux user: 00101100

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. manipulating fgetc while reading a file
    By agentsmith in forum C Programming
    Replies: 1
    Last Post: 04-10-2008, 02:52 PM
  2. character input using fgetc() code???
    By lesrhac03 in forum C Programming
    Replies: 3
    Last Post: 03-27-2008, 11:55 PM
  3. Using fgetc() to read control characters
    By Bnchs in forum C Programming
    Replies: 8
    Last Post: 03-22-2008, 06:23 AM
  4. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 06:33 PM
  5. About getc and fgetc. Please help.
    By Antigloss in forum C Programming
    Replies: 4
    Last Post: 09-28-2005, 05:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21