Close
Close

I am trying to get a structure written as output into a text file stored in the same directory as the executable. I have added the compiler error mes

   Akshit Lonare , IIT Kharagpur

#include <stdio.h>

struct ticket
{
    int trainnum;
    char trainname[30];
    char doj[11];
    char sex;
    unsigned int age;    
    unsigned int fare;
};

void out(struct ticket *t)
{
    FILE *text;
    text = fopen("ticket.txt", "w");
    
    fprintf("Train No. : %d\n", t -> trainnum);
    fprintf("Train Name : %s\n", t -> trainname);
    fprintf("Date of Journey : %s\n", t -> doj);
    fprintf("Sex : %c\n", t -> sex);
    fprintf("Age : %d\n", t -> age);
    fprintf("Fare : %d", t -> fare);
    
    fclose(text);

}


int main()
{
    struct ticket t1;
    
    printf("Train No. :");
    scanf("%d", &t1.trainnum);
    printf("Train Name :");
    gets(t1.trainname);
    printf("Date of Journey (dd/mm/yyyy) :");
    gets(t1.doj);
    printf("Sex :");
    scanf(" %c", &t1.sex);
    printf("Age :");
    scanf("%d", &t1.age);
    printf("Fare :");
    scanf("%d", &t1.fare);
    
    
    out(&t1);
    
    return 0;
}

/*

E:\pd\project\writing to file\dev.cpp    In function 'void out(ticket*)':
18    43    E:\pd\project\writing to file\dev.cpp    [Error] cannot convert 'const char*' to 'FILE* {aka _iobuf*}' for argument '1' to 'int fprintf(FILE*, const char*, ...)'
19    45    E:\pd\project\writing to file\dev.cpp    [Error] cannot convert 'const char*' to 'FILE* {aka _iobuf*}' for argument '1' to 'int fprintf(FILE*, const char*, ...)'
20    44    E:\pd\project\writing to file\dev.cpp    [Error] cannot convert 'const char*' to 'FILE* {aka _iobuf*}' for argument '1' to 'int fprintf(FILE*, const char*, ...)'
21    32    E:\pd\project\writing to file\dev.cpp    [Error] cannot convert 'const char*' to 'FILE* {aka _iobuf*}' for argument '1' to 'int fprintf(FILE*, const char*, ...)'
22    32    E:\pd\project\writing to file\dev.cpp    [Error] cannot convert 'const char*' to 'FILE* {aka _iobuf*}' for argument '1' to 'int fprintf(FILE*, const char*, ...)'
23    32    E:\pd\project\writing to file\dev.cpp    [Error] cannot convert 'const char*' to 'FILE* {aka _iobuf*}' for argument '1' to 'int fprintf(FILE*, const char*, ...)'
 

*/

/*

I have no idea what the error messages mean, so it would be really helpful if someone explains what the problem is, and what a possible solution to it might be.

*/


Answers

  •   

    You were just missing the filename in the syntax of fscanf. It is done on the code below:

    #include <stdio.h>
    
    struct ticket
    {
        int trainnum;
        char trainname[30];
        char doj[11];
        char sex;
        unsigned int age;    
        unsigned int fare;
    };
    
    void out(struct ticket *t)
    {
        FILE *text;
        text = fopen("ticket.txt", "w");
        
        fprintf(text,"Train No. : %d\n", t -> trainnum);
        fprintf(text,"Train Name : %s\n", t -> trainname);
        fprintf(text,"Date of Journey : %s\n", t -> doj);
        fprintf(text,"Sex : %c\n", t -> sex);
        fprintf(text,"Age : %d\n", t -> age);
        fprintf(text,"Fare : %d", t -> fare);
        
        fclose(text);
    
    }
    
    
    int main()
    {
        struct ticket t1;
        
        printf("Train No. :");
        scanf("%d", &t1.trainnum);
        printf("Train Name :");
        //Taking extra character
        getchar();
        gets(t1.trainname);
        printf("Date of Journey (dd/mm/yyyy) :");
        gets(t1.doj);
        printf("Sex :");
        scanf(" %c", &t1.sex);
        printf("Age :");
        scanf("%d", &t1.age);
        printf("Fare :");
        scanf("%d", &t1.fare);
        
        
        out(&t1);
        
        return 0;
    }
    

     



  •   

    #include<stdio.h>
    typedef struct student
    {
            int rno;
            char name[100];
            int age;
    }st;
    void fun(st s)
    {
            FILE *fp;
            fp=fopen("data","w");
            fprintf(fp,"%d",s.rno);
            fprintf(fp,"%s",s.name);
            fprintf(fp,"%d",s.age);
    }


    main()
    {
            st s1;
            printf("enter the roll number\n");
            scanf("%d",&s1.rno);
            printf("enter the name\n");
            scanf("%s",s1.name);
            printf("enter the age\n");
            scanf("%d",&s1.age);
            fun(s1);
    }

     



  •    Klais

    I'm only looking for a writing service that can assist me with my essay. I adore music and am learning to play the guitar in preparation for music school; my dream is to start my own rock band. I need a letter of recommendation to complete my application without difficulty, and I have good musical instrument talents, but I'm still doubtful. I discovered a website called lor residency letter of recommendation where they may purchase letters of recommendation, and they claim that many individuals use their services and are delighted with their work.



  •    seoexpertim

    Super-Duper site! I am Loving it!! Will come back again, Im taking your feed also, Thanks. chat avenue



  •    RyanAndres

    Our Cisco 200-301 dumps is the most reliable solution to quickly prepare for your Cisco Cisco Certified Network Associate. We are certain that our Cisco 200-301 practice exam will guide you to get certified on the first try. Here is how we serve you to prepare successfully. 



  •    frederickgragg

    Introducing the latest advancements in Salesforce Data Cloud! Stay at the forefront of innovation with our 2023 New Questions update, designed to elevate your data management experience. Uncover unparalleled insights and streamline your workflow with cutting-edge features tailored to meet the evolving demands of your business.

    Our Salesforce Salesforce-Data-Cloud New Questions update set is meticulously crafted to address the dynamic landscape of data analytics, ensuring you're equipped with the knowledge to make informed decisions. Dive into new challenges that reflect real-world scenarios, empowering you to master Salesforce Data Cloud with confidence. Elevate your skill set, boost productivity, and harness the full potential of your data.

    Experience the future of data management – upgrade to the 2023 New Questions for Salesforce Data Cloud today and redefine what's possible in the world of business intelligence. Your success starts with staying ahead, and we're here to guide you every step of the way.



Ask Yours
Post Yours
Write your answer