answersLogoWhite

0

How do you write to a file using C?

User Avatar

Anonymous

15y ago
Updated: 8/17/2019

The following example code uses fprintf to save some text to a file.

Code Example:#include #include #define szMY_DATA "Some example data." #define szFILENAME "testfile.txt" int main(void) { FILE *fpMyFile = fopen(szFILENAME, "w"); /* open testfile.txt for */ /* writing */ if(fpMyFile != NULL) /* make sure opening the file was successful */ { /* using fprintf to print szMY_DATA to szFILENAME */ if(fprintf(fpMyFile, szMY_DATA) != strlen(szMY_DATA)) { /* ERROR. fprintf returns how many characters were written, so */ /* if it's not the same as the string length there was an error. */ } if(fclose(fpMyFile)) { /* ERROR. fclose returns 0 if there weren't any errors. */ } } else { /* ERROR. Failed to open the file. */ } return 0; }
User Avatar

Wiki User

15y ago

What else can I help you with?

Related Questions

Write a C program that takes a binary file as input and finds error check using different mechanisms?

write a c program that takes a binary file as input and finds error check using different mechanisms.


How do you write a c program that counts number of blanks in a text file using standard io?

int count_whitespace (FILE* input) { int c, count=0; while (( c = getc(input) ) != EOF ) if ((char) c==' ') ++count; return count; }


Can you write a C program Without turbo C IDE?

Yes you can write a C program without using turbo C IDE. All you need is a text editor like notepad where you can write your codes and after writting them save the file with .c extension. But for compilling and running the code you will be needing turbo C IDE.


How do you read and write video file in java?

you can use inputstream for reading the file java.io.fileinputstream and write the file using outputstream..


How do you write c file arguments?

Using parameters argc and argv, and library functions fopen, fprintf, fclose


A program in c to copy text file?

File Operations in C are accomplished by the use of pointers.Pointers are use to point to a particular memory location.File are read and written by using file pointers.The Keyword FILE is used to declare a file pointer.The Complete Program for writing text to a file and saving it and copying it to a new location is given at http://c-madeeasy.blogspot.com/2011/07/program-in-c-to-write-data-to-file-and.html


When is an object file in c used?

When you write a compiler, assembler or linker in C.


How can you read and write to files in C programming language?

We can read and write to files with the help of file commands in c programming.There are so many commands related to file for read,write,append,delete etc.


How do you store data in c?

Write your data to a data file.


How do you save a text file as c file on mac?

From editor: File / Save As or File / Write To From shell: rename, move, mv


How you can delete the data in the file when you open the file in read or write mode in C programming?

When you open a file in write mode, eg. fp=fopen("filename.txt","w"); the content of the file is deleted.


Write a C program to extract a given word from a file?

program to extract a given word from a file