I wrote a file using C's fwrite
where I wrote a string, followed by some ints, multiple times. I used this code to write:
fwrite(&words,sizeof(char),strlen(words) - 1, outputFile);
fwrite(&nextNum,sizeof(int),1, outputFile);
How can I read my file back into strings and ints?
Also, would it be faster to read an array of ints from the file instead of multiple consecutive ints?
The way you write into the file you lose a critical piece of information: the length of your string. There are generally three ways to handle this:
'\0'
null-terminator used for standard C-stringsOf course, reading back would depend on the technique you chose to write the strings out. But once you are done with the string's variable length complication writing/reading integers (which are always of an exact, known size) should be a breeze.