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

AI Thread Summary
To read specific lines from a file in C using the Dev C++ compiler, start by opening the file with `fopen()` and using `fgets()` to read each line into a buffer. To perform calculations such as counting the number of lines, calculating the average, maximum, minimum, and standard deviation of the numbers in the file, a separate function can be created. This function should process the lines read from the file, converting string data to numerical values using `atof()` or `fscanf()`. It's important to handle potential overflow by using `double` for calculations involving sums. For standard deviation, a formula exists that allows for its calculation using running totals, which can be found in resources like Wikipedia. This approach will facilitate the completion of the assignment effectively.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top