write a c program to read,write and append file using switch case?
Ashvini Goyal
2009-12-12 08:09:33 UTC
actually im tryin to do basics of file handling.....
Five answers:
A
2009-12-15 00:21:04 UTC
Before you can actually read, write, and append a file you must have the standard in and out library as well as the standard library (stdio.h and stdlib.h). Once you have those libraries then you have to create a pointer that will points to the files and then use the fopen() function to open the file and always remember to have fclose() at the end to close the file. Here's a simple IO file.
#include
#include
int main(void)
{
char buffer[100];
FILE *fp;
/* Check if file can be openned */
if( (fp = fopen("somefile.txt", "w+")) )
{
/* Write a character to the text file. */
fputc('A', fp);
/* Write a String to a file. */
fputs("\nHello World!!!\n", fp);
/* Get a string from the text file.
This is a safer approach since
there's limit on how many
characters the function can get. */
fgets(buffer, 99, fp);
/* Closes the file */
fclose(fp);
}
else
printf("Could not open file.");
return 0;
}
You can have the file name as a variable string. You can have:
char filename[50] = "somefile.txt";
Then you do put it in the fopen() function:
fp = fopen(filename, "r");
--------------------------------------------
Here's the different modes you can have in the second parameter of fopen:
"r" Open text files for reading.
"w" Create or override existing file. (Deletes content of file if it exists).
"a" Append; open or create file for writing at end-of-file.
"rb" Open binary file for reading.
"wb" Same as for the writing mode, makes a binary file for writing.
"ab" Append; open of create binary file for writing at end-of-file.
"r+" Open text file for update (read and writing).
"w+" Truncate to zero length or create text file for update.
"a+" Append; open or create text file for update, writing at end-of-file.
"rb+" Open binary file for update (read and writing).
"wb+" Truncate to zero length or create binary file for update.
"ab+" Append; open or create binary file for update, writing at end-of-file.
"r+b" Same as rb+
"r+b" Same as wb+
"a+b" Same as ab+
?
2016-12-25 00:31:22 UTC
1
?
2016-04-28 02:43:10 UTC
If you have a problem to supporting your youngster that's problem examining, no matter what age she\he this program is the thing you need, Children Learning Reading from here https://tr.im/Tg5xQ .
Children Learning Reading is different from other applications since it does not count on training phrases by sight, a way that relies on kiddies recognizing and memorizing phrases by their content and structure. You might have seen (or also purchased!) classes that promote whole term recognition learning, these programs usually require you to sit your youngster in front of a tv or pc check for a significant amount of time in order to allow them to understand phrases that'll allow them to begin their reading journey.
With Children Learning Reading your kid can just need to spend 5\15 minutes at time and he\she can learn to construct the phrase making the whole procedure for learning how to learn far more effectively.
lucier
2016-10-15 11:17:39 UTC
lern too grammer. different than that, a swap case is fairly comparable to an if assertion occasion: int important() { int a; cout << "enter value of a: "; cin >> a; swap{ case a million: cout << "a is a million"; return a million; smash; case 2; cout << "a is two"; return a million; smash; } return 0; }
VinceF
2009-12-12 08:16:29 UTC
so what's your question?
If you're simply lost, start by reading man pages for:
open()
close()
read()
write()
lseek()
and
fopen()
fclose()
fread()
fwrite()
fseek()
That's all you need for basic file handling.
Good luck
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.