Reading from an input file

This is a discussion on Reading from an input file within the C Programming forums, part of the General Programming Boards category; It's been a while since I've had to do any programming at all so I'm back to basics again Basically ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    1

    Reading from an input file

    It's been a while since I've had to do any programming at all so I'm back to basics again

    Basically I want to read a list of values from an input file into an array and print the values out again. My code looks like:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    FILE *infile1;
    
    int main()
    {
    	float f0, A, t0, T, t[10], area, f[1800], g[1800], log[1800], tf=4679.45, ti=0.44;
    	int i;
    	
    	infile1 = fopen("list.txt","r");
    	if(infile1 == NULL) {
    		printf("Could not read file!");
    	}
    	else {
    		for (i=0; i<10; i=i+1) {
    			t[i]=fgetc(infile1);
    			printf("%f\n", t[i]);
    		}
    	}
    return 0;
    }
    (ignore all the float except for t[10])

    The file list.txt simply contains a list of numbers 1 to 10 on separate lines (singly spaced), no other characters. At the moment I just want to get the program to print out these values in the same way but I can't remember how to even do this, how to use fgetc or even if this is the correct function to be using.
    The program clearly finds the file but returns a list of 10 random numbers

    Help on this much appreciated,
    Thanks

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    fgetc() gets a single character.

    You probably want to be using something like fscanf().

    But you definitely want to learn to read the documentation.
    cstdio (stdio.h) - C++ Reference
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

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