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

Click For Summary
SUMMARY

The discussion centers on reading and formatting file data in C, specifically handling a file with five data items per line in the order of float, float, integer, float, and string. The user encounters a "stack smashing detected" error while attempting to read the file into a 20-character array named weatherFile. The user seeks assistance in utilizing the fgets function to read the file and format the output with multiple spaces between data items for improved readability.

PREREQUISITES
  • Understanding of C programming language
  • Knowledge of file handling in C
  • Familiarity with pointers in C
  • Basic understanding of data types: float and integer
NEXT STEPS
  • Learn how to use fgets for reading files in C
  • Research formatting output in C using printf
  • Explore error handling techniques for stack smashing in C
  • Study dynamic memory allocation to handle variable-length data
USEFUL FOR

C programmers, students learning file I/O, and anyone looking to improve their skills in data formatting and error handling in C.

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.
 

Similar threads

Replies
7
Views
3K
Replies
8
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
Replies
7
Views
3K
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K