ok so i know how to program in JAVA..
i am trying to learn C but the language is pretty confusing..

so i want to read from a file..
i created a FILE, numbers.txt ---> this file has int values.
i have code to read the file, now i am confused as to how to manipulate the data from the file?
for instance maybe sum up the data with a for loop and maybe print out the sum to a different file?
do i make n e sense?
sorry guys:(

________C O D E_______________

#include<stdlib.h> // infile

int main(void){
	int sum = 0;
   char filename[] = "numbers.txt";
   FILE *file = fopen ( filename, "r" );
   if ( file != NULL )
      char line [100]; /* or other suitable maximum line size */
      while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
      	 fputs ( line, stdout ); /* write the line */
      fclose ( file );
      perror ( filename ); 
   return 0;

Edited by WaltP: Added CODE Tags

6 Years
Discussion Span
Last Post by abhimanipal

First, always highlight your code and click on the [CODEĀ ] icon at the top of the editor. That will put your code into a special font, etc., so it becomes easily readable.

Second, inside your while loop (which looks good by the way), you need to have some variables and maybe use sscanf() or other functions to get the values from the line buffer, into the variables you need to work with. Just printing out the line, won't do the summation you want.


Oh ok sounds good I will work on it tomorrow when I am working.. Thank u for your support I really appreciate it!


What is the format of the numbers in your file ? Depending upon that you may have to do some string parsing ....

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.