Writing values to a file in c




















Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search.

Working on a program dealing with file stream. I am wanting to edit a line of text that is stored in a text file. To my understanding, the best way to do this would be to copy everything from the original file and over-write it in a new file, making my edits along the way.

I haven't been able to find a clear example online, and was wondering if I could see one. How are we doing? Please help us improve Stack Overflow.

Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. It returns the written character written on success otherwise EOF if there is an error. The function fputs writes the string s to the output stream referenced by fp.

It returns a non-negative value on success, otherwise EOF is returned in case of any error. Try the following example. If it is not, then before proceeding, you must create this directory on your machine. When the above code is compiled and executed, it creates a new file test. Let us read this file in the next section. The fgetc function reads a character from the input file referenced by fp.

The return value is the character read, or in case of any error, it returns EOF. The functions fgets reads up to n-1 characters from the input stream referenced by fp. To write a C structure to a file, use fwrite. In this C Tutorial , we have learnt how to write variables or char array or stream of data into a file using different function available in C programming with examples.

C Programming. C Ternary Operator. C File Operations. The sprinff call was not quite correct - fixed that too. You could use the possibly more efficient atoi but the subsequent string processing to determine the end of the string will probably be more expensive - sprintf returns the number of characters written so you do not have to scan for the end of the string. Stepan : I have made a number of other corrections - make sure you have my latest version.

The problem with posting code fragments without the ability to test it. Nemo : I added test results. The block write is significantly faster as I expected based on experience ; nearly twice as fast on 32 bit code, for 64 bit code, the difference was not as great but still significant. I suggest that "completely wrong" may have been overstating it; I'd have accepted "in need of verification", but you were at lest right to query it without test results.

Show 6 more comments. It might be operating system and implementation specific. Don't forget to explicitly ask your compiler to optimize, e. Basile Starynkevitch Basile Starynkevitch 1. Well when I send the solution, I send only two files: file.

The solution should be in commands even in the description of the problem, is statement, that fprintf is too slow and we should try something else to speed things up. Sign up or log in Sign up using Google. Sign up using Facebook.

Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back



0コメント

  • 1000 / 1000