URGENT. Need help editing some C code. I have done most of the code it just needs the following added:
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;
}
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:
We access the structure variable in this way
URGENT. Need help editing some C code. I have done most of the code it just...
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> #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 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 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 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 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 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 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 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;
//
-----------------------------------------------------------------------
//...
Please I need help in C language, I am trying to modify the code per the below instructions, but I am getting errors. Can't fgure it out. The attempted code modification and data file is privided below, after the instructions. Thank you. Instructions: 1. First, add a function named printMsg that displays a message, a greeting, or an introduction to the program for the user. Add a statement that calls that function from the main function when your program starts....