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<stdio.h>
#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 );
   }
   else
   {
      perror ( filename ); 
   }
   return 0;
}

Recommended Answers

All 3 Replies

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 ....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.