Question

URGENT. Need help editing some C code. I have done most of the code it just...

URGENT. Need help editing some C code. I have done most of the code it just needs the following added:

  1. takes an input file name from the command line;
  2. opens that file if possible;
  3. declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float);
  4. declares an array of C structs (i.e. the same struct type declared in point c);
  5. reads a record from the file and stores the values read into the appropriate struct variable at the appropriate array index (for that record);
  6. continues reading each record into a struct (as in point 1e), and stores each struct containing the 3 values into the array of structs declared in point d;
  7. closes the file when all records have been read.


CODE:

#include
#include
#include

int main(int argc, char* argv[]) {

int line_count = 0;
int index, i;

typedef struct info{
char name[100];
int age;
float wage;
} info;

info hr[10];

if (argc != 2)
printf("Invalid user_input!\n");
else {
FILE* contents = fopen (argv[1], "r");
struct info in;
  
if (contents != NULL) {
printf("File has been opened successfully.\n\n");
while (fscanf(contents, "%s %d %f\n",names[line_count], &age[line_count], &wage[line_count]) != EOF) {
printf("%s %d %f\n", names[line_count], age[line_count], wage[line_count]);
line_count++;
}
printf("\nTotal number of lines in document are: %d\n\n", line_count);
fclose(contents);
printf("Please enter a name: ");

fgets(user_input, 30, stdin);
user_input[strlen(user_input)-1] = '\0';

index = -1;
for(i = 0; i < line_count; i++){
if(strcmp(names[i], user_input) == 0){
index = i;
break;
}
}
if(index == -1)
printf("Name %s not found\n", user_input);
else{
      while(fread(&in, sizeof(struct info), 1, contents))
        printf("Name: %s, Age: %d, Wage: %.2f\n", user_input, in.age, in.wage);
}
} else {
printf("File cannot be opened!");
}
}

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The implementation of given features is shown in the below C code:

#include<stdio.h>
#include<string.h>

struct info{
char names[100];
int age;
float wage;
};

int main(int argc, char* argv[]) {

int line_count = 0;
int index, i;

struct info hr[10];

if (argc != 2)
printf("Invalid user_input!\n");
else {
FILE* contents = fopen (argv[1], "r");
struct info in;
  
if (contents != NULL) {
printf("File has been opened successfully.\n\n");
while (fscanf(contents, "%s %d %f\n",hr[line_count].names, &hr[line_count].age, &hr[line_count].wage) != EOF) {
printf("%s %d %f\n", hr[line_count].names, hr[line_count].age, hr[line_count].wage);
line_count++;
}
printf("\nTotal number of lines in document are: %d\n\n", line_count);
fclose(contents);
printf("Please enter a name: ");
char user_input[100];
fgets(user_input, 30, stdin);
user_input[strlen(user_input)-1] = '\0';

index = -1;
for(i = 0; i < line_count; i++){
if(strcmp(hr[i].names, user_input) == 0){
index = i;
break;
}
}
if(index == -1)
printf("Name %s not found\n", user_input);
else{
while(fread(&in, sizeof(struct info), 1, contents))
printf("Name: %s, Age: %d, Wage: %.2f\n", user_input, in.age, in.wage);
}
} else {
printf("File cannot be opened!");
}
}

return 0;
}

You have made some minor mistakes that I have corrected in above code:

  • Inclusion of Header files
  • Declaring structure outside the main function
  • And accessing the value of structure in correct way

We access the structure variable in this way

  1. hr[line_count].names //for accessing name
  2. hr[line_count].age //for accessing age
  3. hr[line_count].wage //for accessing wage
Add a comment
Know the answer?
Add Answer to:
URGENT. Need help editing some C code. I have done most of the code it just...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h>...

    Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<dirent.h> #include<stdio.h> #include<stdlib.h> void do_ls(char []); int main(int argc,char *argv[]) { if(argc == 1) do_ls("."); else while(--argc){ printf("%s:\n",*++argv); do_ls(*argv); } } void do_ls(char dirname[]) { DIR *dir_ptr; struct dirent *direntp; if((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr,"ls1:cannot open %s\n",dirname); else { while((direntp = readdir(dir_ptr)) != NULL) printf("%s\n",direntp->d_name); closedir(dir_ptr); } } ____________________________ code 2: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> void show_stat_info(char *,...

  • How would I change the following C code to implement the following functions: CODE: #include <stdio.h>...

    How would I change the following C code to implement the following functions: CODE: #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc != 2) printf("Invalid input!\n"); else { FILE * f = fopen (argv[1], "r"); if (f != NULL) { printf("File opened successfully.\n"); char line[256]; while (fgets(line, sizeof(line), f)) { printf("%s", line); } fclose(f); } else { printf("File cannot be opened!"); } } return 0; } QUESTION: Add a function that uses fscanf like this:...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • -I need to write a program in C to store a list of names (the last...

    -I need to write a program in C to store a list of names (the last name first) and age in parallel arrays , and then later to sort them into alphabetical order, keeping the age with the correct names. - Could you please fix this program for me! #include <stdio.h> #include <stdlib.h> #include <string.h> void data_sort(char name[100],int age[100],int size){     int i = 0;     while (i < size){         int j = i+1;         while (j < size){...

  • I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves...

    I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves the array of structures to file. It is already implemented for you. // You should understand how this code works so that you know how to use it for future assignments. void save(char* fileName) { FILE* file; int i; file = fopen(fileName, "wb"); fwrite(&count, sizeof(count), 1, file); for (i = 0; i < count; i++) { fwrite(list[i].name, sizeof(list[i].name), 1, file); fwrite(list[i].class_standing, sizeof(list[i].class_standing), 1, file);...

  • Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with...

    Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with a new string. * Append an s to the end of your input string if it's not a keyword. * Insert a period at the end, if you have an empty input string do not insert a period Problems: //Why does my code stop at only 2 input strings //If I insert a character my empty string case works but it's still bugged where...

  • IN C ONLY As mentioned earlier there are two changes we are going to make from...

    IN C ONLY As mentioned earlier there are two changes we are going to make from lab 5, The file you read into data structures can be any length. studentInfo array will be stored in another struct called studentList that will contain the Student pointer and current length of the list. Sometimes data can be used in structs that correlate between variables so it's convenient to store the data in the same struct. Instead of tracking a length variable all...

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

  • Need this in C The starter code is long, if you know how to do it...

    Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...

  • I need help with this C code Can you explain line by line? Also can you...

    I need help with this C code Can you explain line by line? Also can you explain to me the flow of the code, like the flow of how the compiler reads it. I need to present this and I want to make sure I understand every single line of it. Thank you! /* * Converts measurements given in one unit to any other unit of the same * category that is listed in the database file, units.txt. * Handles...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT