Thread: Reading a file

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    1

    Reading a file

    I got a question for example i have in txt file like '
    DAY#3,CUSTOMER_4, COKE, MILK, SHAMPOO
    DAY#6,CUSTOMER_5, IRON-MAN FIGURE, UNICORN, PRINCESS DRESS
    ' my code works fine until customer and i got struct like day[],cutomer[], products[] and i want to put those day and stuff on those arrays but i couldnt save those products on product array just one and then goes to the other line and also space between some strings like 'Tooth Brush' code reads it as Tooth and Brush i couldnt figure out this problem so code has to be like 'Day: Customer: Products: ' any help please

    my code is like :

    Code:
    while (fgets(rec, BUFSIZ, fp) != NULL) {
            //rec[strlen(rec) - 1] = '\0';
            i = 0;
            token = strtok(rec, ", ");
            while (token != NULL) {
                flds[i] = token;
                i++;
                token = strtok(NULL, ", ");
            }
    
            strcpy(product.day, flds[k]);
            strcpy(product.customer, flds[k+1]);
            //strcpy(product.products, flds[3]);
            
            printf("Day = %s\n", &product.day);
            printf("Customer = %s\n", &product.customer);
    
            for(k=2;k<15;k++){
                strcpy(product.products, flds[k]);
                printf("Products = %s\n", product.products);
            }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,498
    > printf("Day = %s\n", &product.day);
    > printf("Customer = %s\n", &product.customer);

    Well you don't normally use & when printing stuff.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-27-2013, 12:24 PM
  2. Replies: 0
    Last Post: 03-21-2013, 03:31 PM
  3. Replies: 3
    Last Post: 11-28-2012, 09:16 AM
  4. Replies: 3
    Last Post: 07-17-2011, 03:51 AM
  5. Reading flat file and generating tagged file
    By AngKar in forum C# Programming
    Replies: 4
    Last Post: 03-24-2006, 08:29 AM

Tags for this Thread