outputting to another file

This is a discussion on outputting to another file within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to take information from one file and write parts of it to another. The file that ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    34

    outputting to another file

    Hi, I am trying to take information from one file and write parts of it to another. The file that I'm taking information from looks something like this:

    230943455...etc (about 100 digits or more)
    100101990610...etc
    101101990711....etc
    101101990610....etc
    284923938....etc

    The first line and the last line is some kind header and trailer information that I don't need and I would like to skip them. The lines in the middle contain information that I want to make readable. After the first 6 digits you can see a date..990610 (oct 6,1999)(there is more to get in the file but if I can get the date, I feel like I can get the rest) Then I'm trying to take that information and write it to another file. The code that I wrote creates the new file but there is nothing written to it. I hope someone can take a look and tell me how write this correctly. To you more advanced programmers, I must warn you...the code you are about to see may be disturbing and might keep you up at night!
    Code:
    /*
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<string.h>
    #include<ctype.h>
    #include<mem.h>
    #define max 400
    
    FILE *test;
    FILE *test1;
    void parse(char *){
    }
    
    void main(void){
    
         char word[max];
         char *ptr;
         char date1[20],date2[20],date3[20];
         char slash[20]={"/"};
         int date; //the address is numbers...int, char....how do I declare?
         test1=fopen("c:\\mark.txt","w");
         test=fopen("c:\\test.txt","r");   //pointer must go to a location?
         fgets(word,max,test);
    while(!feof(test)){
    
            strcat(date2,slash);
            strcat(date2,date3);
            strcat(date2,slash);
            strcat(date2,date1);
            memset(word,'\0', sizeof(word));
         parse(word);
         ptr=word;
         ptr=ptr+6;
           memcpy(&date,ptr,6);
    fclose(test);
    fclose(test1);
    
    
     }
     //system("pause");
    } 
    */
    I have not attempted to add code to skip the first and last lines because I have no idea how to begin. Hammer and jabrams have given me some pointers on this; however, I have been unsuccessful. Any help would be greatly appreciated! Mcorn

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    first, it's bad practise to name your FILE pointers names like test and test1 on a regular basis. use in or out to make things clearer.

    i think you know how to skip the first and last lines
    while not end of file
    read in 6 chars using fgets or fgetc and discard the info
    read 2 chars, convert to year (probably using atoi) and stick it in a year array
    use the above method to move the next 4 chars into date and month

    ...and so on ...
    get the newline and discard it.
    }

    right now i can't see any file writing commands in your program, so that's the first place i would check if you don't plan on rewriting it

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >void main(void)
    main returns an int, the correct usage therefore is
    >int main(void)
    and return something at the end of the function.

    >while (!feof(test))
    Don't control your loop with feof(), instead use the return code from the function that's doing the read (fgets() or whatever).

    >coding style
    Layout your code neatly, with the correct indentation. As soon as I did this to your code, I spotted an problem immediately:
    Code:
    while (!feof(test))
        {
            strcat(date2, slash);
            strcat(date2, date3);
            strcat(date2, slash);
            strcat(date2, date1);
            memset(word, '\0', sizeof(word));
            parse(word);
            ptr = word;
            ptr = ptr + 6;
            memcpy(&date, ptr, 6);
            fclose(test);
            fclose(test1);
        }
    As you can see, at the end of the loop your are closing both your file streams. Why is that??!

    >char date2[20];
    >strcat(date2, slash);
    You can't use strcat() straight onto an uninitialised variable. You don't know what date2 contains when the program starts, therefore your strcat() will cause all sorts of havoc.

    >test1 = fopen("c:\\mark.txt", "w");
    Its best to check that fopen() worked before you start using the file stream for reading/writing. fopen() will return NULL if it fails.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Unregistered
    Guest

    reply to Hammer

    I'm not sure why?(fclose) I am new to programming and I really don't have a clue. I found an example in a book and I'm trying to use this example to make a program that would be useful to me if I could make it work. The book said that I should use fclose to close the files...It said that it was analogous to finalizing a music cd after recording.

    I'm able to copy the file to my c:\ drive and it names it c:\\mark.txt (or whatever I named it)...but when I try to make it put specific information on the mark.txt file I'm totally blowing it at that point! I've tried changing the code using the pointers that you gave me. I looked in the area that you pointed out and tried different things such as removing the fclose and changing the while loop to fgets() still unable to find the problem. If you don't mind, keep those tips coming. I really love the challenge that programming has given me, but it is very discouraging at times!
    I've been using a book that claims to teach it in 21 days...it's been 3 months now and I still don't have it!
    Thanks for your help. Mcorn.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    34
    I guess that would be the thing to do.
    Code:
    /*
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<string.h>
    #include<ctype.h>
    #include<mem.h>
    #define max 400
    
    FILE *test;    // this should not be named test for now I'll leave it
    FILE *test1;
    
    void parse(char *){
    }
    
     int  main(void){
    
    	  char word[max];
    	  char *ptr;
    	  char date1[20],date2[20],date3[20];
    	  char slash[20]={"/"};
    	  int date; //is numbers...int, char....how do I declare?
    	  test1=fopen("c:\\mark.txt","w");
    	  test=fopen("c:\\test.txt","r");   //pointer must go to a location?
    	  fgets(word,max,test);
    
    	  //while(!feof(test)){
    	  fgets(test);
    		strcat(date2,slash);
    		strcat(date2,date3);
    		strcat(date2,slash);
    		strcat(date2,date1);
    		memset(word,'\0', sizeof(word));
    		parse(word);
    		ptr=word;
    		ptr=ptr+6;
    		memcpy(&date,ptr,6);
    	  //}
    
     //system("pause");
    }
    */
    The fgets under the while loop is not used right because of the errors I get. What is the correct way?
    Thanks, mcorn

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    34
    I just re-read what Hammer posted referring to the strcat line.
    I thought that that line would go to the file and skip the first 6 characters (ptr=ptr+6) and grab the date there. I'm not understanding what he means by the uninitialized value? Mcorn

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >I'm not understanding what he means by the uninitialized value.
    OK, have a look at this explanation:

    You create a variable called date, it's a char array, size 20:
    >char date2[20];
    This gets you 20 bytes of memory, and in that memory can be anything (it's an area that the OS has given you, and has already been used by another program.) Therefore, you must be careful about how you first use this variable.

    In your example code, you are using it incorrectly when using strcat(). This is because strcat() will append data to an existing string in a char array. As you have no idea what is in your date2 array to start with, you cannot append to it (safely).

    You must empty the array first. A quick way to fix this would be to use something like this:
    >date2[0] = '\0';
    which will set the first byte of the array to \0, which is a string terminator. Then when you use strcat(), you are doing so safely.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    To sum up Hammer's explanation:
    You can't be so sure that other programmers are as safe as you are.
    You can't be sure they cleaned up after themselves.
    They could very well have left clutter all over the place.
    This is clutter that you are going to walk in, and trip over.
    The world is waiting. I must leave you now.

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    34
    Thank you Hammer and Shadow.
    I understand what you're saying and I assume this is a habit I should get into anytime I use strcat?
    Another question...Is using ='\0' the same as using =NULL ?

  10. #10
    Registered User Commander's Avatar
    Join Date
    Sep 2001
    Posts
    804
    yup, or atleast i hope so! cause that's what i use
    oh i'm sorry! i didn;t realize my fist was rushing to meet ur face!

    MSN :: commander_android@hotmail.com []*[]

  11. #11
    Registered User
    Join Date
    Jul 2002
    Posts
    34

    I'm making it worse.

    Each thing that I try is making it worse. Could someone look at this and at least tell me if I'm somewhat heading in the right direction?
    Code:
    FILE *test;
    FILE *test1;
    void parse(char *){
    }
    
    int main(void)
    {
      char word[max];
      char date1[20],date2[20],date3[20];
      //char date1[20]='\0',date2[20]='\0',date3[20]='\0';
      char *ptr;
      char date[20];
      memset(date,'\0',sizeof(date));
      memset(date1,'\0',sizeof(date1));
      memset(date2,'\0',sizeof(date2));
      memset(date3,'\0',sizeof(date3));
      test1=fopen("c:\\output.txt","w");
      test=fopen("c:\\10111115.txt","r");
      fgets(word,max,test);
      //ptr=word;
    
      while(!feof(test))
      {
      fprintf(test1,"%s",word);
      fgets(word,max,test);
      memset(word, '\0', sizeof(word));
      parse(word);
      ptr=ptr+6;
      memcpy(date,ptr,6);
      }
      //fclose(test);
      //fclose(test1);
    }
    Thank you, Mcorn

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > //ptr=word;
    This should be inside the while loop

    Ok, back to basics

    Code:
    int main ( ) {
        char    buff[BUFSIZ];
        FILE *fin = fopen("words.txt","r");
        FILE *fout= fopen("results.txt","w");
        while ( fgets(buff,BUFSIZ,fin) != NULL ) {
            fputs( buff, fout );
        }
        fclose( fin );
        fclose( fout );
        return 0;
    }
    This simply copies the input file to the output file.

    All you need to do now is replace the
    &nbsp;&nbsp;&nbsp;&nbsp;fputs( buff, fout );
    with your own code which extracts the data of interest.

  13. #13
    Registered User
    Join Date
    Jul 2002
    Posts
    34
    Thank you Salem! this will get me on the right track. Mcorn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21