0

I'm working on a project for university, the basic idea is that I want to check if a file is empty.

void main() {
 FILE* file = fopen("stocksData.csv", "w+");
 fseek(file, 0, SEEK_END);
 unsigned long len = (unsigned long)ftell(file);
 if (len == 0) { //check if the file is empty
 fclose(file);
 importCarStock();
 }
}

If it is empty, I want it to call this function:

void importCarStock() {
 FILE* file = fopen("stocksData.csv", "w");
 fwrite(carStock, sizeof(char), sizeof(carStock), file);
 fclose(file);
 
}

And write this array to the file:

int carStock[10] = { 5, 7, 10 };

But the only thing it writes to the file is two small blocks. Any idea what is wrong with this? Thanks in advance.

asked Dec 10, 2020 at 22:31
7
  • 6
    Opening the file in w+ mode will empty it. Use r mode. Commented Dec 10, 2020 at 22:33
  • 2
    5, 7, 10 are ASCII codes of something that might look like "two small blocks" in a text viewer. Commented Dec 10, 2020 at 22:34
  • 2
    Your function is misnamed. Writing to a file is exporting, not importing. Commented Dec 10, 2020 at 22:34
  • You're writing a binary file. If you want to write a CSV file, you need to format the numbers. Use sprintf() or fprintf(). Commented Dec 10, 2020 at 22:34
  • 3
    Do you want to write the raw binary data to the file? Or as text? Right now you're kind of mixing it, as you open the file in text-mode, but write raw binary data. There are plenty of fil I/O tutorials on the Internet, and any decent book should contain a couple of chapters about it as well. I suggest you take some time to study a little bit more. Commented Dec 10, 2020 at 22:35

1 Answer 1

1

fwrite(carStock, sizeof(char), sizeof(carStock), file);

This is the problem. fwrite writes binary data. If you have the array { 5, 7, 10 }, then it will write the bytes 05 00 00 00 07 00 00 00 0A 00 00 00 (assuming a little-endian system). You are seeing 2 blocks because of the bytes 05 and 07, which are control characters (the 0s are null characters and 0A is a line feed).

You need a for loop:

for (int i = 0; i < sizeof carStock / sizeof carStock[0]; i++)
 fprintf(file, "%d ", carStock[i]);

If you want to output numbers to a file, you need to decide whether you want to see these numbers with a text editor or a hex editor. If you store them as text, it's easier to see them with a text editor like Notepad, but reading and writing costs more time and is more error-prone if string to number (and vice versa) conversions are not performed correctly. If you store them in binary, you won't be able to see them with a text editor, you will just see gibberish because your text editor tries to interpret binary data as ASCII/Unicode characters, however they take less space and are faster and easier to read and write.

Here are the bytes of the file generated with the for loop (you can check them with a hex editor):

35 20 37 20 31 30 20

And here are the bytes interpreted as text:

5 7 10

answered Dec 10, 2020 at 23:00
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! That seemed to do the trick, only problem now is that it prints a bunch of 0s after the numbers up to the size of the array, but at least it works.
The reason it prints a bunch of zeroes is because your array has more elements than you fill it with. You are only filling 3 values, but the array has 10 elements. The for loop I wrote writes the entire array. If you want only some of the values (like 3, in your example), you will need to keep track of this number in another variable and replace it in the for loop.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.