Thread: Can someone explain what I'm doing wrong in writing the file?

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    117

    Can someone explain what I'm doing wrong in writing the file?

    I'm given a file to read that each line has a name, item, and amount in it such as this..

    Jeff,clothes,23
    Barry,book,78
    Casey,phone,24
    Jeff,book,47

    Here is my homework site for more detail...
    http://omega.uta.edu/~darin/CSE1320/1320-hw05.pdf

    But, the approach I have looked at is to read the file line by line and split up the name, item, and number into their own arrays. Then our teacher suggested for each name, create a file of their own with the items and number of items in it.

    Then create an array of structures to keep track of the names of the files created.

    My code right now doesn't have the structures yet. But I'm trying to append the item and amount to the files I create. What it is doing apparently in this code is writing over the file with the newest item and amount.

    For example,

    instead of having the file Jeff filled with....
    clothes,23
    book,47

    it just has book,47.

    How would I fix this to where it writes in every line?

    Code:
    int main(void){
    
    
        char buffer[50];
        char name[50];
        char item[50];
        int amount[50];
        FILE *fp;
        FILE *fp2;
        char filename[50];
    
    
        
        if ( (fp = fopen("names.csv", "r" )) == NULL )
        {
           printf("Couldn't open file\n");
           exit(1); 
        }
        
        while(fgets(buffer, sizeof(buffer), fp))
        {
           sscanf(buffer,"%[^,] ,%[^,], %s", &name, &item, &amount);
           strcat (name, ".txt");
    
    
           if( (fp2 = fopen(name, "w")) == NULL )
           {
               printf("unable to open %s\n", filename);
               exit( 1 );
           }
         
           fprintf(fp2, "item is %s, amount is %s\n", &item, &amount);
           fclose(fp2);
         
           fclose(fp);
        }
    getchar();
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You open the inner file for write (which destroys old contents), as opposed to say append (which doesn't).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    117
    Ok cool, silly error on my part. That works now. I'll come back if I need anymore help

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    117
    double post, but I figured I'd use this one instead of starting a new one.

    One of the requirements and easiest ways to do this is ...

    "You will need to keep track of the names of the files created; for this, create an array of
    structures."

    My question is how would I do this? I'm guessing I build a structure that just has....

    Code:
    struct names
    {
           char name[15];
    };
    and then somehow while reading the file, I need to use that structure for each name?

    Don't really understand this completely. Thanks for any advice.

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    In class has your instructor shown how to access members within a structure? Think of a many boxes on a shelf and each box contains listings of names. So you need to be able to open up and view each box, ie box1, box2 and read the contents within those. Hope this analogy was not to confusing or made it worse for you.

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    117
    Yea, he's shown us how to access the members within a structure. But the only examples he gives is the hard coding into the structure such as this example.

    Code:
    #include <stdio.h>#include <string.h>
    
    
    struct pet {
        char name[30];
        char type[30];
    };
    
    
    int main(void)
    {
        struct pet friends[2] = {{"Spot", "dog"},
                                 {"Rex", "cat"}};
        int i;
    
    
        for(i = 0; i < 2; i++)
            printf("%s is a %s\n", friends[i].name,
                                   friends[i].type);
    }

    So if I understand it right, this is going through each array in the struct, and assigns the first string to name, and the second string to type. If that's right, then how would I store my array of names I get via the struct?

    I'm assuming that this means I have to use this while the file is being read since the names keep changing. So it needs to read the name like it is doing now, and then store it via the struct right? Or am I wrong in that approach? If I'm right, what would be a good syntax for that? Trying to understand the right way to code it. I may be totally off but what I'm using now and trying to figure out how to make it work is this code...

    Code:
    struct names{
           char name[15];
    };
    then this is when the first file is being read.

    Code:
    struct names all[]={name};
    Last edited by mgracecar; 02-29-2012 at 03:35 PM.

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Below link may help in regards to c file I/O. The key here is that you need to know how your file is set up to delimit it accordingly to parse out your names and types to assign their respective structure members, perhaps within a loop of sorts.
    C File I/O Tutorial - Cprogramming.com

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    117
    Ok, I just read through that and used the quiz too, will bookmark for more use.

    One last question though..

    Would you personally say that how I initially grab the file would be the best way for this project? I grab the file, and split it into it's three different parts while reading it and then create a file for each of these names and add there items and amount in there.

    I'm leaving out the part where I need to keep track of how many names right now. If this part is right/a good way to go, then I will be able to work on the structure next.

    code is right here and works fine as far as creating the files it needs to create.

    Code:
    int main(void){
    
    
        char buffer[50];
        char name[15];
        char item[99];
        int amount;
        FILE *fp;
        FILE *fp2;
        int i;
    
    
        
        if ( (fp = fopen("names.csv", "r" )) == NULL )
        {
           printf("Couldn't open file\n");
           exit(1); 
        }
        
        while(fgets(buffer, sizeof(buffer), fp))
        {
           sscanf(buffer,"%[^,] ,%[^,], %d", &name, &item, &amount);
           strcat (name, ".txt");
    
    
           //create file for names
           if( (fp2 = fopen(name, "a")) == NULL )
           {
               printf("unable to open %s\n", name);
               exit( 1 );
           } 
           fprintf(fp2, "%s,%d\n", &item, amount);
           fclose(fp2);
         }
         fclose(fp);
    }

  9. #9
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    That looks good. For a clue at line 23, 'strctat (name, ".txt");', you can simply loop through your structure members and assign the name and types directly to them at that point. You already have a loop going on with your while statement and have the variables defined, all you will need to do is increment your structure...

    some very crude code below
    Code:
    ...
    23  names[i].name = name;
    24  names[i].item = item;
    25  names[i].amount = amount;
    26  i++;
    ...
    Last edited by slingerland3g; 02-29-2012 at 04:42 PM.

  10. #10
    Registered User
    Join Date
    Feb 2012
    Posts
    117
    Ok, I'll continue to work on it.
    I want to thank you for all your help though. You helped me understand this a lot more.

  11. #11
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Thanks, but I have to confess that I have learned a ton as well from these forums. Trying to help others helps me too!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Could someone explain what I did wrong with this?
    By Paroxysm in forum C++ Programming
    Replies: 7
    Last Post: 01-07-2008, 03:08 PM
  2. Replies: 4
    Last Post: 08-07-2007, 06:07 PM
  3. Could somebody please explain to me where I went wrong?
    By tsvguy in forum C++ Programming
    Replies: 15
    Last Post: 11-23-2005, 07:25 AM
  4. can someone explain what is wrong here...
    By Lynux-Penguin in forum C Programming
    Replies: 2
    Last Post: 05-13-2002, 11:39 AM
  5. Whats wrong with my program?(writing data into a file)
    By Ruflano in forum C++ Programming
    Replies: 1
    Last Post: 03-02-2002, 08:19 PM