Thread: Can I write & read file in same program?

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    10

    Can I write & read file in same program?

    Here is my problem:
    Write a C program that will read and display every second character in the file named test.dat.

    Here is the cod I have so far:

    Code:
    #include <stdio.h>
    int main()
    {
    	
         	     FILE *outfile;
                         char data[40] = "kTjHdIjSo kInSg xAc rCv hPhRyOmGqRaAeM";
    	     outfile = fopen("test.dat","w");
    	     if (outfile == NULL)
    	      {
    		    printf("\nFailed to open the file.\n");
    	
    	      }
    	     fprintf(outfile,"%-40s\n",data[40]);
    	     fclose(outfile);
    	
    	
    	
    	FILE *infile;
    	infile = fopen("test.dat","r");
    	if (infile == NULL)
    	{
    		printf("\nFailed to open the file.\n");
    		
    	}
    	
    	while (fscanf(infile, "%s",data) !=EOF)
    		printf("%-40s\n",data);
    	fclose(infile);
    	
    	
    	
    	
    	return 0;
    }
    I was wondering can I make the test.dat file at the beginning of the program and then access it right after that? Also does anyone have any ideas on how to read the 2nd character? I was trying to spell out a message once I got it to work. I was thinking a FOR loop but not sure how to make it work. Can I increment by 2 and if so how? Thanks for helping me out guys. Also how do I get this to indent so the code is easier to read? It looks fine in the message box.

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    10
    Thanks for the help Salem!

    One more question though, I tried a FOR loop to scan every 2nd number but I guess I didn't set it up right. I just kept getting everything instead of the message I tried to embed. Any ideas on how to make this work? Here's the new code:

    Code:
    #include <stdio.h>
    int main()
    {
    	int i;
    	FILE *infile;
    	FILE *outfile;
        char data[40] = "kTjHdIjSo kInSg xAc rCv hPhRyOmGqRaAeM";
    	outfile = fopen("test.dat","w");
    	if (outfile == NULL)
    	{
    		printf("\nFailed to open the file.\n");
    	
    	}
    	fprintf(outfile,"%-40s\n",data);
    	fclose(outfile);
    	
    	
    	
    	
    	infile = fopen("test.dat","r");
    	if (infile == NULL)
    	{
    		printf("\nFailed to open the file.\n");
    		
    	}
    	
    	while (fscanf(infile, "%s",data) !=EOF)
    		printf("%-40s\n",data);
    	fclose(infile);
    	
    	
    	
    	
    	return 0;
    }

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Any ideas on how to make this work?
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       char data[] = "kTjHdIjSo kInSg xAc rCv hPhRyOmGqRaAeM";
       /*
        * Create the file. Write 'data' string followed by newline.
        */
       FILE *file = fopen("test.dat", "w");
       if ( file != NULL )
       {
          fprintf(file, "%s\n", data);
          fclose(file);
       }
       /*
        * Read the file one char at a time; write every other char.
        */
       file = fopen("test.dat", "r");
       if ( file != NULL )
       {
          int i = 0; /* skip the first char */
          for ( ;; )
          {
             int ch = fgetc(file); /* read each char from file */
             if ( ch == EOF )
             {
                break;
             }
             if ( i ) /* print every other char */
             {
                putchar(ch);
             }
             i = !i; /* toggle 'i' to print every other char */
          }
          putchar('\n');
          fclose(file);
       }
       return 0;
    }
    
    /* my output
    THIS IS A C PROGRAM
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Last program!!
    By buckwheat88 in forum C++ Programming
    Replies: 12
    Last Post: 01-17-2006, 12:31 PM
  4. Replies: 5
    Last Post: 02-01-2003, 10:58 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM