Thread: Adding structure data in txt file not working

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    47

    Adding structure data in txt file not working

    Hello friends,

    i am trying to add structure data to txt file but it gives errors

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include<windows.h>
    /* Use of typedef in Structure */
    
    
    
    
    typedef struct home_address2{
    
    
    char local_street2[100];
    char town2[60];
    char city2[60];
    char country2[60];
    }myaddress2;
    
    
    
    
    
    
    int main(){
    
    
    FILE *ptr;
    ptr=fopen("address.txt","a");
    
    
    
    
        printf("\t\t\t\xB2\xB2\xB2\ ADD RECORD  \xB2\xB2\xB2\xB2");
        printf("\n\n\nEnter Street Address \t\t\Town\t\t\City\t\tCountry :");
        scanf("%s %s %s  %s",&myaddress2.local_street2,&myaddress2.town2,&myaddress2.city2,&myaddress2.country2);
        while(fscanf(ptr,"%s %s %s  %s\n",&myaddress2.local_street2,&myaddress2.town2,&myaddress2.city2,&myaddress2.country2)!=EOF)
        {
    
    
         printf("data added");
        }
         fclose(ptr);
    
    return 0;
    
    
    }
    Last edited by sash_007; 06-29-2019 at 08:09 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Are you trying to write data to the file or read data from the file?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Do you think myaddress2 is a variable or a type?
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Do you realize that myaddress2 is a type not an instance of your structure?

  5. #5
    Registered User
    Join Date
    May 2019
    Posts
    47
    ok i am confused



    what is the difference between

    Code:
    struct home_address2{
    
    char local_street2[100];
    char town2[60];
    char city2[60];
    char country2[60];
    }myaddress2;
    Code:
    typedef struct home_address2{
    
    char local_street2[100];
    char town2[60];
    char city2[60];
    char country2[60];
    }myaddress2;
    type and variable/instance not same?
    when i use this code
    Code:
    struct home_address2{
    
    char local_street2[100];
    char town2[60];
    char city2[60];
    char country2[60];
    }myaddress2;
    it runs but nothing is added to address.txt file yet?

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    fscanf is a input function not an output function.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    May 2019
    Posts
    47
    yes i understand tht fscanf is input function

    i just want to take data from user and store it in address.txt file

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by sash_007 View Post
    yes i understand tht fscanf is input function

    i just want to take data from user and store it in address.txt file
    Please look for the line of code the writes to the file!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    what is the difference between
    The first snippet is creating an instance of the structure home_address2 named myaddress2. It is the same as the following:

    Code:
    // Define the structure.
    struct home_address2{ 
    char local_street2[100];
    char town2[60];
    char city2[60];
    char country2[60];
    };
    
    // Now create an instance of that structure.
    struct home_address2 myaddress2;
    The second snippet is creating an alias for the home_address2 type named myaddress2. This is a type (because of the typedef) not an instance of the structure.

    Code:
    struct home_address2{
    char local_street2[100];
    char town2[60];
    char city2[60];
    char country2[60];
    };
    
    // Create an alias of the home_address2 struct.
    typedef struct home_address2 myaddress2;
    
    // Now create an instance of the structure using the typedef.
    myaddress2 my_address;  // Notice that the struct keyword is not needed.

  10. #10
    Registered User
    Join Date
    May 2019
    Posts
    47
    okthanks

    this works
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include<windows.h>
    /* Use of typedef in Structure */
    
    
    
    
    struct home_address2{
    
    
    char local_street2[100];
    char town2[60];
    char city2[60];
    char country2[60];
    }myaddress2;
    
    
    
    
    
    
    int main(){
    
    
    FILE *ptr;
    ptr=fopen("address.txt","a");
    
    
    
    
        printf("\t\t\t\xB2\xB2\xB2\ ADD RECORD  \xB2\xB2\xB2\xB2");
        printf("\n\n\nEnter Street Address \t\t\Town\t\t\City\t\tCountry :");
        scanf("%s %s %s  %s",&myaddress2.local_street2,&myaddress2.town2,&myaddress2.city2,&myaddress2.country2);
        fscanf(ptr,"%s %s %s  %s\n",&myaddress2.local_street2,&myaddress2.town2,&myaddress2.city2,&myaddress2.country2);
        fprintf(ptr,"%s %s %s  %s\n",&myaddress2.local_street2,&myaddress2.town2,&myaddress2.city2,&myaddress2.country2);
    
    
    
    
         printf("data added");
    
    
        fclose(ptr);
    return 0;
    
    
    }
    Last edited by sash_007; 06-29-2019 at 10:00 PM.

  11. #11
    Registered User
    Join Date
    May 2019
    Posts
    47
    this works but

    after i enter street name
    town the cursor jump to country
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include<windows.h>
    /* Use of typedef in Structure */
    
    
    
    
    struct home_address2{
    
    
    char local_street2[100];
    char town2[60];
    char city2[60];
    char country2[60];
    }myaddress2;
    
    
    
    
    
    
    int main(){
    
    
    FILE *ptr;
    ptr=fopen("address.txt","a");
    
    
    
    
        printf("\t\t\t\xB2\xB2\xB2\ ADD RECORD  \xB2\xB2\xB2\xB2");
        printf("\n\n\nEnter Street Address: ");
        scanf(" %s",&myaddress2.local_street2);
        printf("\n\n\nEnter Town: ");
        scanf(" %s ",&myaddress2.town2);
        printf("\n\n\nEnter City: ");
        scanf(" %s",&myaddress2.city2);
        printf("\n\n\nEnter Country: ");
        scanf(" %s",&myaddress2.country2);
    
    
        fscanf(ptr,"%s %s %s  %s\n",&myaddress2.local_street2,&myaddress2.town2,&myaddress2.city2,&myaddress2.country2);
        fprintf(ptr,"%s %s %s  %s\n",&myaddress2.local_street2,&myaddress2.town2,&myaddress2.city2,&myaddress2.country2);
    
    
    
    
    
    
    
    
        fclose(ptr);
        printf("data added");
    return 0;
    
    
    }
    i added space here
    Code:
     scanf(" %s ",&myaddress2.town2);
    but still same thing happens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading data from a file and adding them to a list
    By Alex Predova in forum C Programming
    Replies: 1
    Last Post: 06-01-2014, 11:27 PM
  2. Replies: 4
    Last Post: 08-25-2013, 05:09 AM
  3. Adding data to existing data in an input file?
    By matthayzon89 in forum C Programming
    Replies: 4
    Last Post: 11-20-2010, 11:23 PM
  4. adding data to the middle of a file from another file
    By xixpsychoxix in forum C Programming
    Replies: 9
    Last Post: 09-07-2006, 11:00 AM
  5. writing data(adding new records) to the input file
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 07:51 PM

Tags for this Thread