How Can I Read Specific Lines from a File in C Language Using Dev C++ Compiler?

  • Context: C/C++ 
  • Thread starter Thread starter edosqclbe699
  • Start date Start date
  • Tags Tags
    File Lines Reading
Click For Summary

Discussion Overview

The discussion centers around reading specific lines from a text file in C using the Dev C++ compiler. Participants explore how to read a file containing numerical data and perform calculations such as determining the number of lines, average, maximum, minimum, and standard deviation of the numbers. The scope includes programming techniques and functions relevant to file handling and data processing in C.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • One participant seeks guidance on reading specific lines from a text file and performing calculations on the data.
  • Another participant provides a basic code snippet for reading lines from a file using fgets().
  • A different participant specifies the need to create a function in a separate file to return various statistical values from the data.
  • One reply suggests using atof() or fscanf() to convert strings to numbers and mentions a formula for calculating standard deviation that avoids re-reading numbers.
  • Concerns are raised about potential overflow with large numbers, recommending the use of doubles for calculations.

Areas of Agreement / Disagreement

Participants generally agree on the need for a function to handle the calculations and the importance of correctly reading the file, but there is no consensus on the specific implementation details or methods to be used.

Contextual Notes

Participants have not fully resolved how to implement the function for calculations, and there are various approaches suggested without a clear agreement on the best method. The discussion also reflects uncertainty regarding handling large numbers and the appropriate data types to use.

Who May Find This Useful

Readers interested in file handling and data processing in C, particularly those working on similar homework assignments or projects involving numerical data analysis.

edosqclbe699
Messages
4
Reaction score
0
I have the Dev C++(I am writing in C language) compiler and I need help on how to read in specific lines in a file and complete calculations on them. I need to read in a .txt file (I know how to do this, however I don't know how to read in the specific lines in a file) that has a column of about 20 numbers. For my assignment I need to determine the number of lines in the file, the average value of the numbers, the max and min of the numbers, and the standard deviation of the numbers. So far in class we have covered functions, pointers, and loops. I am assuming I need to use a function but I have no idea how. Can someone generally tell me the way to read in the lines into the program so I can compute these 4 things.
 
Technology news on Phys.org
This should get you started:

int main() {
FILE *file;
char line[80];
file = fopen("numbers.txt", "r");

if(file) {
while(fgets(line, 80, file)) {
// "line" contains characters from current line here
}

fclose(file);
}
return 0;

}
 
Last edited:
I actually need to create a function in a separate file that will return the values of the number of lines in the file, the average of the values, the max and min of the values, and the standard deviation of the values. Sorry for not being more specific before but, can anyone give me a little advice on how to do it this way.
 
It's a homework question? The above should get you started.
To convert the line to a number look at atof() or fscanf()
There is a formula to calculate the standard deviation from running totals without having to re-read the numbers subtracting the mean. Look on wiki for details.
If these are large numbers be careful about overflow, you should probably use doubles for the sums.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 29 ·
Replies
29
Views
4K
Replies
81
Views
8K
  • · Replies 6 ·
Replies
6
Views
4K
Replies
35
Views
8K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 6 ·
Replies
6
Views
12K