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

In summary, the person is asking for help on how to read in a file, determine the number of lines, average, max, and min in the file, and the standard deviation of the values. They are also asking for a function to do this.
  • #1
edosqclbe699
4
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
  • #2
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:
  • #3
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.
 
  • #4
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.
 

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

What is the purpose of "Reading in lines from a file" in scientific research?

The purpose of reading in lines from a file is to extract data or information from a file for analysis and interpretation in scientific research. This allows researchers to process large amounts of data efficiently and accurately.

What types of files can be used for "Reading in lines from a file" in scientific research?

Any type of file that contains text data can be used for reading in lines, such as .txt, .csv, .tsv, or .dat files. These files can be generated from various sources, including experiments, surveys, or simulations.

What are the steps involved in "Reading in lines from a file" in scientific research?

The steps typically involved in reading in lines from a file include opening the file, reading or parsing the data, storing the data in a suitable format, and closing the file. The data may then be further processed or analyzed depending on the research objectives.

What are some common challenges encountered when "Reading in lines from a file" in scientific research?

One common challenge is dealing with large files that contain a high volume of data. This can lead to slow processing times and memory issues. Another challenge is ensuring the accuracy and integrity of the data, as errors in the data can significantly impact the results of the research.

What are some best practices for "Reading in lines from a file" in scientific research?

Some best practices for reading in lines from a file include thoroughly understanding the data and its format, properly handling and cleaning the data, and using efficient and optimized code for processing. It is also important to document the steps taken and any transformations made to the data for reproducibility and transparency.

Similar threads

  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
2
Views
514
  • Programming and Computer Science
2
Replies
35
Views
1K
  • Programming and Computer Science
Replies
1
Views
371
  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
786
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
18
Views
1K
Back
Top