Hey you guys Im not really good with structures and I was wonderingif someone co
ID: 3618905 • Letter: H
Question
Hey you guys Im not really good with structures and I was wonderingif someone could help me with this problem. I have to prompt theuser for a text file (random.txt) and then store the contentsinto a structure so I can do so file manipulation. But I dont knowhow to put the contents into a structure, if someone could help meout with that im pretty much good to go. Here is what the contentslook like in the file: random.txt ;Gates, William, H, 1.89 Jobs, Steven, P, 2.56 Brin, Sergey, , 3.89 Page, Lawrence, , 3.77 Buffett, Warren, E, 3.34
My code: #include <stdio.h> #include <stdlib.h> #define TEN 10 #define THREE 3 #define TWENTY 20 #define FIFTY 50 #define ONE_THOUSAND 1000
typedef struct { char first_name[TEN]; char middle_name[THREE]; char last_name[TWENTY]; double users_gpa[TEN];
} format_of_file;
int main () { /* Declarations: File I/O, */ FILE *inp, *outp; char first_file[FIFTY], second_file[FIFTY]; format_of_file organized[ONE_THOUSAND]; /* Get users inputs */ printf("Enter the input file name: "); scanf("%s", first_file); printf("Enter the output file name: "); scanf("%s", second_file); inp = fopen(first_file, "r"); outp = fopen(second_file, "w"); /* Test if both files for validity */ if (inp == NULL) { perror("Invalid File"); exit(EXIT_FAILURE); } if (outp == NULL) { perror("Invalid File"); exit(EXIT_FAILURE); }
return 0; }
Thanks you guys. I just need help figuring out how to put thatcontent into a structure. This is different for me because ivenever dealt with , before in a text file. random.txt ;
Gates, William, H, 1.89 Jobs, Steven, P, 2.56 Brin, Sergey, , 3.89 Page, Lawrence, , 3.77 Buffett, Warren, E, 3.34
My code: #include <stdio.h> #include <stdlib.h> #define TEN 10 #define THREE 3 #define TWENTY 20 #define FIFTY 50 #define ONE_THOUSAND 1000
typedef struct { char first_name[TEN]; char middle_name[THREE]; char last_name[TWENTY]; double users_gpa[TEN];
} format_of_file;
int main () { /* Declarations: File I/O, */ FILE *inp, *outp; char first_file[FIFTY], second_file[FIFTY]; format_of_file organized[ONE_THOUSAND]; /* Get users inputs */ printf("Enter the input file name: "); scanf("%s", first_file); printf("Enter the output file name: "); scanf("%s", second_file); inp = fopen(first_file, "r"); outp = fopen(second_file, "w"); /* Test if both files for validity */ if (inp == NULL) { perror("Invalid File"); exit(EXIT_FAILURE); } if (outp == NULL) { perror("Invalid File"); exit(EXIT_FAILURE); }
return 0; }
Thanks you guys. I just need help figuring out how to put thatcontent into a structure. This is different for me because ivenever dealt with , before in a text file. Gates, William, H, 1.89 Jobs, Steven, P, 2.56 Brin, Sergey, , 3.89 Page, Lawrence, , 3.77 Buffett, Warren, E, 3.34
My code: #include <stdio.h> #include <stdlib.h> #define TEN 10 #define THREE 3 #define TWENTY 20 #define FIFTY 50 #define ONE_THOUSAND 1000
typedef struct { char first_name[TEN]; char middle_name[THREE]; char last_name[TWENTY]; double users_gpa[TEN];
} format_of_file;
int main () { /* Declarations: File I/O, */ FILE *inp, *outp; char first_file[FIFTY], second_file[FIFTY]; format_of_file organized[ONE_THOUSAND]; /* Get users inputs */ printf("Enter the input file name: "); scanf("%s", first_file); printf("Enter the output file name: "); scanf("%s", second_file); inp = fopen(first_file, "r"); outp = fopen(second_file, "w"); /* Test if both files for validity */ if (inp == NULL) { perror("Invalid File"); exit(EXIT_FAILURE); } if (outp == NULL) { perror("Invalid File"); exit(EXIT_FAILURE); }
return 0; }
Thanks you guys. I just need help figuring out how to put thatcontent into a structure. This is different for me because ivenever dealt with , before in a text file. #include <stdio.h> #include <stdlib.h> #define TEN 10 #define THREE 3 #define TWENTY 20 #define FIFTY 50 #define ONE_THOUSAND 1000
typedef struct { char first_name[TEN]; char middle_name[THREE]; char last_name[TWENTY]; double users_gpa[TEN];
} format_of_file;
int main () { /* Declarations: File I/O, */ FILE *inp, *outp; char first_file[FIFTY], second_file[FIFTY]; format_of_file organized[ONE_THOUSAND]; /* Get users inputs */ printf("Enter the input file name: "); scanf("%s", first_file); printf("Enter the output file name: "); scanf("%s", second_file); inp = fopen(first_file, "r"); outp = fopen(second_file, "w"); /* Test if both files for validity */ if (inp == NULL) { perror("Invalid File"); exit(EXIT_FAILURE); } if (outp == NULL) { perror("Invalid File"); exit(EXIT_FAILURE); }
return 0; }
Thanks you guys. I just need help figuring out how to put thatcontent into a structure. This is different for me because ivenever dealt with , before in a text file.
Explanation / Answer
here is the code block that you can be used to read from file tostructure if (inp == NULL) { printf("Invalid File"); exit(EXIT_FAILURE); } else { while (!feof(inp)) { fscanf(inp, "%s", organized[counter].first_name); fscanf(inp, "%s", organized[counter].middle_name); fscanf(inp, "%s", organized[counter].last_name); fscanf(inp, "%f",&organized[counter].users_gpa); counter++; } } For more information you can refer the following example, http://www.daniweb.com/forums/thread92034.html http://www.daniweb.com/tutorials/tutorial45806.html Here is the Full working code sample: #include #include #define TEN 10 #define THREE 3 #define TWENTY 20 #define FIFTY 50 #define ONE_THOUSAND 100 struct format_of_file { char first_name[TEN]; char middle_name[THREE]; char last_name[TWENTY]; char users_gpa[TEN]; }; int counter = 0; int main(void) { FILE *inp, *outp; char first_file[FIFTY], second_file[FIFTY]; struct format_of_file organized[ONE_THOUSAND]; printf("Enter the input file name: "); scanf("%s", first_file); printf("Enter the output file name: "); scanf("%s", second_file); inp = fopen(first_file, "r"); outp = fopen(second_file, "w"); if (inp == NULL) { printf("Invalid File"); exit(EXIT_FAILURE); } else { while (!feof(inp)) { fscanf(inp, "%s", organized[counter].first_name); fscanf(inp, "%s", organized[counter].middle_name); fscanf(inp, "%s", organized[counter].last_name); fscanf(inp, "%s", organized[counter].users_gpa); counter++; } fclose(inp); } for(int i = 0;iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.