How Can You Correctly Read and Format File Data in C?

AI Thread Summary
The discussion revolves around reading and formatting file data in C, specifically a file containing five data items per line in the order of float, float, integer, float, and string. The user is encountering a "stack smashing detected" error after compiling their code, indicating potential issues with memory management or buffer overflows. They express confusion regarding the use of pointers and the fgets function, which they attempted to learn independently. Additionally, the user seeks advice on how to format the output to include multiple spaces between data items for better readability. Assistance is requested to resolve these issues and improve the program's functionality.
NDiggity
Messages
53
Reaction score
0

Homework Statement


Ok, here is the other question that's giving me a headache. There is a file which holds 5 data items per line and a minimum of 1 line. The data is always in the order float float integer float string and each element is separated by a space. So we need to ask the user for a file name and then we need to output the data to the user in a user friendly way. It let's me compile but after it prints out everything i get a stack smashing detected error which I have no clue as to its meaning. We just learned pointers and how to open and read files and stuff and I am very confused. We were never taught fgets in class so I tried to teach myself on the net so I am a little confused. It let's me put the whole contents of the file I'm reading into a 20 character array i made called weatherFile?

I also somehow need to print off each individual data item with several spaces in between instead of single spaces like how the original file is to make it more user friendly and I'm not sure how to do that with the way I have it written now. So if someone can take a look at the program I have so far and help me, it would be very greatly appreciated. Thanks for your time! :)

Program: http://rafb.net/p/FaBfWS88.html
 
Last edited by a moderator:
Physics news on Phys.org
Your paste on rafb.net has expired. Please repost.
 
Back
Top