Reading file line by line

This is a discussion on Reading file line by line within the C Programming forums, part of the General Programming Boards category; Hi guys i'm kind of new to C and am trying to read a text file that has a number ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    6

    Reading file line by line

    Hi guys i'm kind of new to C and am trying to read a text file that has a number stored on each line like below.

    17767

    9158

    39017

    18547

    56401

    23807

    37962

    22764

    7977


    This is the code i have so far, it's just attempting to read thefirst line then i plan to tak it from there, but when i run it i get some funny results rather than getting 17767 I get
    177
    18
    91

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    
    
    int main (void)
    {
    
    FILE *ptrFilein;
    char cTempread;
    int iLoop;
    unsigned char ucBuffer[1000];
      if((ptrFilein = fopen("random.txt","r"))==NULL)
    	{
    	printf("Error opening file");
    	return(-1);
    	}
    
      cTempread = fgetc(ptrFilein);
      
      for (iLoop = 0; fgetc(ptrFilein)!='\n'; iLoop++)
    	{
    	ucBuffer[iLoop] = cTempread;
    	cTempread = fgetc(ptrFilein);
    	}
    
      printf("The values from the file were %s",&ucBuffer[0]);
    
    }
    Any help would be greatly appreciated Cheers
    Chris

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    > #include <malloc.h>
    This isn't a standard header.

    This should print every line in the file, once you've opened the file.
    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, ptrFilein ) != NULL ) {
      printf( "%s", buff );
    }

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by chrisjmoss
    but when i run it i get some funny results rather than getting 17767 I get
    177
    18
    91
    Notice how the output is every other character?

    Code:
      cTempread = fgetc(ptrFilein);
      
      for (iLoop = 0; fgetc(ptrFilein)!='\n'; iLoop++)
    	{
    	ucBuffer[iLoop] = cTempread;
    	cTempread = fgetc(ptrFilein);
    	}
    You are reading the value and comparing it with the newline character, but then you discard the character you just read.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. reading words line by line from a file
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 05-04-2008, 01:34 AM
  3. Reading random line from a text file
    By helloamuro in forum C Programming
    Replies: 24
    Last Post: 05-03-2008, 11:57 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

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