Words of Wisdom:

"In 3 words, I can sum up everything I have learned about Life.. It goes on" - Xmisfitsx310

File Handling in C

  • Date Submitted: 12/21/2010 10:18 AM
  • Flesch-Kincaid Score: 87.2 
  • Words: 386
  • Essay Grade: no grades
  • Report this Essay
File Handling in C

1. Implement following code, which allows to read the file “r.txt” contents:
int main()
{
      FILE *stream;
      char list[128];
      int numread;
      if( (stream = fopen( "r.txt", "r" )) != NULL )
      {
      numread = fread( list, sizeof(char), 128, stream );
              printf( "Number of cir elements read = %d\n", numread );
              printf("%s ",list);
              fclose( stream );
      } else
      printf( "File r.txt could not be opened\n" );
      return 0;
}
2. What happen if the number of chars in “r.txt” will be greater/smaller than the size of
  list? Modify the code to be resistant to the size (number of chars) of the file.
3. Use the fseek function to display last 10 chars from „r.txt”.
4. Use the fgetc function to display first and last three chars from „r.txt” file.
5. Use the fgets function to perform the same operations as in step 4.
6. What is the difference between functions fgetc(), fgets() and fread()?
Task 2. File writing
1. Implement following code, which enable writing to the file:
int main()
{
      FILE *stream;
      char list [6]="proba";
      int numwritten;
      if( (stream = fopen( "myfile.txt", "w+t" )) != NULL )
      {
              numwritten = fwrite(list, sizeof(char), 5, stream );
              printf( "Wrote %i items\n", numwritten );
              fclose( stream );
      }else
              printf( "Problem opening the file\n" );
      return     0;
}
2. Write the program that reads the content of the file and save it in another one.
3. Use fputc function to put “--!” at the beginning and “!—-“ at the end of the file.
4. Use the fputs function to perform the same operations as in step 3.
5. What is the difference between functions fputc(), fputs() and fwrite()
Exercise
  1. Write the program which converts “*.html” files into “*.txt” files. The idea
      is to remove all html tags.
  2. Create file „dane_we.txt”,...

Comments

Express your owns thoughts and ideas on this essay by writing a grade and/or critique.

  1. No comments