Help Scanning File Data In C

In summary, the conversation revolves around a homework question where the user needs to read data from a file and output it in a user-friendly manner. The file contains 5 data items per line and the data is always in a specific order, separated by spaces. The user is getting a "stack smashing detected" error and is confused about pointers, opening and reading files, and using fgets. They also need help with formatting the output to have several spaces between data items instead of just one. They are seeking assistance with their program and appreciate any help provided.
  • #1
NDiggity
54
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
  • #2
Your paste on rafb.net has expired. Please repost.
 
  • #3


I can understand how challenging programming can be, especially when learning new concepts like pointers and file handling. It is important to first make sure that your code is syntactically correct and free of any errors. In this case, the stack smashing error could indicate a memory issue, possibly caused by using an array that is too small to hold all the data from the file.

Additionally, when reading data from a file, it is important to check for any potential errors or invalid data. One way to do this is by using the fgets function, which reads a line from a file and stores it in a string. You can then use string manipulation functions to extract the individual data items from the string.

To make the output more user-friendly, you can use the printf function to format the data before printing it to the screen. For example, you can use the %10f format specifier to print a float with 10 spaces between each item.

Overall, it is important to carefully review your code and make sure it is logically sound and free of any potential errors. It may also be helpful to seek assistance from a classmate or a tutor to better understand the concepts and techniques involved in this task. Good luck!
 

What is file scanning in C?

File scanning in C refers to the process of reading data from a file and storing it in a variable within a C program. This allows the program to access and manipulate the data within the file.

How do you scan a file in C?

To scan a file in C, you first need to open the file using the fopen() function. Then, you can use the fscanf() function to read data from the file and store it in a variable. Finally, you should close the file using the fclose() function when you are finished reading from it.

What is the difference between fscanf() and fgets() in C?

fscanf() and fgets() are both functions used for reading data from a file in C. The main difference is that fscanf() reads formatted data, while fgets() reads a string of characters up to a specified delimiter. Additionally, fscanf() will continue reading data from a file until it reaches a specified format, while fgets() will stop reading once it reaches a new line.

What is the purpose of the feof() function in C?

The feof() function in C is used to check if the end of a file has been reached. It returns a non-zero value if the end of file has been reached, and 0 if there is still more data to be read from the file.

How can you handle errors while scanning a file in C?

To handle errors while scanning a file in C, you can use the ferror() function to check for errors during the scanning process. Additionally, you can use the errno variable to get specific error codes for debugging purposes. It is also important to check if the file was successfully opened using the fopen() function before attempting to scan it.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Programming and Computer Science
Replies
1
Views
538
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
16
Views
2K
  • Programming and Computer Science
Replies
1
Views
279
  • Programming and Computer Science
Replies
1
Views
694
  • Programming and Computer Science
Replies
11
Views
995
Back
Top