Very basic File I/O help

This is a discussion on Very basic File I/O help within the C Programming forums, part of the General Programming Boards category; I have a text file with one column of numbers. Eventually these numbers need to be added, averaged etc...but for ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    20

    Very basic File I/O help

    I have a text file with one column of numbers. Eventually these numbers need to be added, averaged etc...but for right now I'm just trying to read them in, then print them out. My main problem is reading until EOF. Since I don't know how long the file will be...

    Is there a way to fix this? Or a better way? Also, will I be able to do things like find the max value, min value, average etc using this format?

    example of the text file:

    43
    23
    746
    543
    76
    2
    654
    ....etc

    Here's an example of the code I'm trying to write. This doesn't work properly though.

    Code:
    #include <stdio.h>
    #include <conio.h>
    void main()
    {
    	FILE *datafile;
    	int data[9999];
    	int x,s;
    	int z=0;
    	char path[80];
    
    	printf("Please enter the location of the file to be opened: ");
    	scanf("%s",&path);
    	
    	datafile=fopen(path,"r");
    
    if(datafile == NULL)
    {
    	puts("Error opening file");
    }
    else
    {
    	while(data != EOF)
    	{
    		fscanf(datafile,"%i",&data[s]);
    		s++;
    		z++;
    	}
    	
    	puts("Data: ");
    	
    	for(s=0;s<z;s++)
    	{
    		printf("%i\n",data[s]);
    	}
    
    }
    getch();
    }

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    Read this completely: Tutorial on C File I/O

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    20
    Are you referring to using fgetc instead of fscanf? Won't fgetc only get one character at a time? Including all of the spaces and returning a char instead of an int? Because I need to do math on these numbers?

    Please help I'm a novice when it comes to C.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    20
    Anyone?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by NewbGuy View Post
    Anyone?
    We're still trying to figure out why you're checking data (the address in memory of your array) against EOF.

    It's true that the FAQ page linked above assumes you know how scanf works. That doesn't appear to be true. (As in: scanf shouldn't use &path, and you need to read more about scanf to see what it does in case of eof.) If you had looked at the FAQ, you might have seen something interesting.

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Remove that 'data' check against EOF, use fgets() to read line by line (you can use 'data' buffer), break out of loop if fgets() returns NULL (it's EOF), else remove '\n' from read buffer (with strchr() maybe), call atoi() to get number out of it - and do anything you like with it...
    Last edited by rasta_freak; 08-02-2008 at 05:36 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. Basic File I/O.... Can somebody help?
    By Spitball in forum C++ Programming
    Replies: 0
    Last Post: 07-24-2004, 04:34 AM

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