Thread: File I/O lab

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    4

    File I/O lab

    I am just stuck on the syantax I know exactly what I need it to do but unsure how to syntactical out put it to a file made a comment on it below //

    Code:
    #include<stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char* argv[])
    {
    
    FILE *ptr_mail,out_mail;
    char sentence[1000];
    char sent[1000];
     int i=0,pos=1;
    ptr_mail =fopen(argv[1],"r");
    if (!ptr_mail)
      {  
      return 1;
      }
    //gets input from file prints by the sentence until AT_EOS
     while (fgets(sentence,1000, ptr_mail)!=NULL)
       {
    
         if(sentence[i]=='F'&& sentence[i+1]=='r' &&sentence[i+2]=='o'&&sentence[i+3]=='m' && sentence[i+4]==' ' )
           {
         //how do I put this all in oneline and too a file ?
         printf("%d",pos);
         printf(" ");
         printf("%s",sentence);
         //need to figure out how to put it to output file
         int fputs ( const char, FILE *stream );
         out_mail= fopen(outputFilename, "w");
         
          pos++;
        
           }      
         
       }
    
    
        fclose(ptr_mail);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You may want to check out the fprintf() function to replace your printf().

    Jim

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    4
    That makes sense it would put whatever in fprintf(...) to output but how do I actually declear it right now I have these long lists of errors b/c it doesn't like
    int fputs ( const char, FILE *stream );

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why are you trying to redefine fputs? Line 27 in the above code is a function definition, not a function call. Plus you are trying to define it differently than how it is defined in stdio.h.

    Jim

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    4
    Its something my TA said i should use so I threw it in there other then that no idea lol.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    4
    Code:
    #include<stdio.h>
    #include <stdlib.h>
    #include <iostream>
    
    int main(int argc, char* argv[])
    {
    
    FILE *ptr_mail,*out;
    char sentence[1000];
    char sent[1000];
     int i=0,pos=1;
    ptr_mail =fopen(argv[1],"r");
    if (!ptr_mail)
      {  
      return 1;
      }
    //gets input from file prints by the sentence until AT_EOS
     while (fgets(sentence,1000, ptr_mail)!=NULL)
       {
    
         if(sentence[i]=='F'&& sentence[i+1]=='r' &&sentence[i+2]=='o'&&sentence[i+3]=='m' && sentence[i+4]==' ' )
           {
    //alright I think I have fprintf working now I cant figure out the syantx to put it to the Isteam or just a file.
         out  = fopen(output.txt, "w");
    
         if(out != NULL)
           {
             fprintf(out,"%d",pos);
             fprintf(out," ");
             fprintf(out,"%s",sentence);
           }
         //need to figure out how to put it to output file
         
         
         
          pos++;
        
           }      
         
       }
    
    
        fclose(ptr_mail);
        return 0;
    }

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I suggest you look the documentation for fopen(), pay particular attention to the open modes. What happens when you open a file with the "w" specifier? Why are you continually opening the file in your loop without closing it? Why do you have the fopen() call inside your loop?

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 09-25-2011, 12:22 AM
  2. Replies: 3
    Last Post: 07-17-2011, 03:51 AM
  3. Replies: 3
    Last Post: 03-08-2010, 02:43 PM
  4. Replies: 3
    Last Post: 11-21-2006, 07:26 PM
  5. Replies: 4
    Last Post: 07-06-2006, 02:53 AM

Tags for this Thread