1

I am writing a program which outputs a file. This file has two parts of the content. The second part however, is computed before the first. I was thinking of creating a temporary file, writing the data to it. And then creating a permanent file and then dumping the temp file content into the permanent one and deleting that file. I saw some posts that this does not work, and it might produce some problems among different compilers or something.

The data is a bunch of chars. Every 32 chars have to appear on a different line. I can store it in a linked list or something, but I do not want to have to write a linked list for that.

Does anyone have any suggestions or alternative methods?

2
  • How much data is there, and what is the nature of the data? Commented Oct 23, 2011 at 21:15
  • Well the file can be as big as it can. The nature of the data is just 0's and 1's represented as chars. Commented Oct 23, 2011 at 22:08

2 Answers 2

4

A temporary file can be created, although some people do say they have problems with this, i personally have used them with no issues. Using the platform functions to obtain a temporary file is the best option. Dont assume you can write to c:\ etc on windows as this isnt always possible. Dont assume a filename incase the file is already used etc. Not using temporary files correctly is what causes people problems, rather than temporary files being bad

Is there any reason you cannot just keep the second part in ram until you are ready for the first? Otherwise, can you work out the size needed for the first part and leave that section of the file blank to come back to fill in later on. This would eliminate the needs of the temporary file.

Sign up to request clarification or add additional context in comments.

6 Comments

Can you be more concrete than asserting that "some people do say" that there are problems with temporary files? A file is a file. If you can write the main output file, you can write a temporary file. The correct location of a temporary file may be platform dependent, but that is not a great reason to avoid temporary files.
Sorry, i guess it comes down to how you use them. I personally have no issues with temporary files. Using the platform functions to obtain a temporary file is the best option. Dont assume you can write to c:\ etc on windows as this isnt always possible. Dont assume a filename incase the file is already used etc. As I say, its not using them correctly that causes people problems, rather than temporary files being bad
Much better. Why not move that comment up into your answer?
Well ideally I can create a linked list which stores each 32 characters in one node. However, I do not want to write a linked list. This will take time and what I am doing is pretty complicated I do not want to waste time writing a linked list, and C does not support a build in list. So what would be a better way to store those characters? Any list like implementation in C would require me to write that data structure from scratch myself, hence me suggesting using a temporary file. If I use a temporary file, what would I need to do/suggestions to avoid issues?
I am writing it on a Mac and I want it to run on Linux. Can I still use temp files in this case?
|
1

Both solutions you propose could work. You can output intermediate results to a temporary file, and then later append that file to the file that contains the dataset that you want to present first. You could also store your intermediate data in memory. The right data structure depends on how you want to organize the data.

As one of the other answerers notes, files are inherently platform specific. If your code will only run on a single platform, then this is less of a concern. If you need to support multiple platforms, then you may need to special case some or all of those platforms, if you go with the temporary file solution. Whether this is a deal-breaker for you depends on how much complexity this adds compared to structuring and storing your data in memory.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.