C programming
The program will require the following structure:
struct _data {
char *name;
long number;
};
The program will require command line arguments:
int main(int argv, char **argc) {
Where argv is the number of arguments and argc is an array
holding the arguments (each is a string). Your program must catch
any case where no command line arguement was provided and print
a warning message (see below).
You MUST include/use the following functions, defined as follows:
int SCAN(FILE *(*stream)) - this function will open the file/stream
and return an integer indicating how many lines there are. Note that
I need to pass stream, which is a pointer, by reference. So I am
passing this as a pointer to a pointer.
struct _data *LOAD(FILE *stream, int size) - this function will
rewind file, create the dynamic array (of size), and read in the
data, populating the _data struct dynamic array. Note that stream
is passed by value this time. The function then returns the populated
array of struct.
void SEARCH(struct _data *BlackBox, char *name, int size) - this function
will get the dynamic array of struct passed to it, the name we are looking
for, and the size of the array. This function will then search the dynamic
array for the name. See below for examples.
void FREE(struct _data *BlackBox, int size) - this function will free up
all of the dynamic memory we allocated. Take note of the number of times
malloc/calloc were called, as you need to free that same number.
Finally, the data file will be called hw5.data and will beformated as:
ron 7774013
jon 7774014
tom 7774015
won 7774016
HINTS:
------
Functions that will make things much easier:
getline()
feof()
strtok()
atoi()
* Make sure you read the man pages for each. They have some "gotchas".
SAMPLE RUNS:
------------
Case 1 - No command line argument provided.
[rmarsh@chef junk]$ ./CS230-5
*******************************************
* You must include a name to search for. *
*******************************************
Case 2 - Provided name is NOT in the list.
[rmarsh@chef junk]$ ./CS230-5 joe
*******************************************
The name was NOT found.
*******************************************
Case 3 - Provided name is in the list.
[rmarsh@chef junk]$ ./CS230-5 tom
*******************************************
The name was found at the 2 entry.
*******************************************
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct _data {
char *name;
long number;
};
int SCAN(FILE *(*stream)) {
int lines = 0;
(*stream) =
fopen("/home/ramkumar/Study/Chegg/1115.txt","r"); // read
mode
if( (*stream) == NULL )
{
perror("Error while opening the file.\n");
return
0;
}
while(!feof((*stream)))
{
int ch = fgetc((*stream));
if(ch == '\n')
{
lines++;
}
}
rewind(*stream);
return lines;
}
struct _data *LOAD(FILE *stream, int size){
char line[256] = {'\0'};
struct _data * items = (struct _data
*)malloc(size*(sizeof(struct _data)));
int index = 0;;
while (fgets(line, sizeof(line), stream) !=
NULL){
index++;
if(index == size){
break;
}
//Break each line by
spaces
char * name = strtok(line,"
");
//get name
int nameLenght =
strlen(name);
struct _data * item = (items +
index);
item->name = (char *)
malloc(nameLenght+1);
memset(item->name,'\0',nameLenght+1);
strcpy(item->name,name);
//get number
item->number =
atol(line+nameLenght+1);
}
return items;
}
void SEARCH(struct _data *BlackBox, char *name, int size) {
for(int i = 0 ; i < size ; i++){
if( strcmp( (BlackBox
+ i)->name,name) == 0){
printf("he name was found at the %d entry.", (i+1));
return;
}
}
printf("The name was NOT found.");
}
void FREE(struct _data *BlackBox, int size) {
for(int i = 0 ; i < size ; i++){
struct _data * item =
(BlackBox + i);
//Free Name String
free
(item->name);
//Free item
free(item);
}
}
int main(int argc, char *argv[])
{
if(argc ==0){
printf("* You must
include a name to search for. *");
return 0;
}
FILE * fp;
SCAN(&fp);
struct _data * items = LOAD(fp,4);
SEARCH(items,argv[0],4);
FREE(items,4);
return 0;
}
C programming The program will require the following structure: struct _data { char *name; long number;...
Command line input In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like Linux and are passed in to the program from the operating system. To use command line arguments in the program, it must first understand the full declaration of the main function, which until now has accepted no arguments. In fact, main can accept two arguments: one argument is number of command line...
I need help programming this question using C language. This program uses input data entered in command line at the same time when the command or .exe file is entered. They are called command-line arguments. Because everything entered in command line is taken as a string, these arguments including the command itself or the .exe file name are stored in an array of char pointers, each pointer points to the first character of a string (i.e., argument). The inputs can...
C Language Programming. Using the program below - When executing on the command line only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...
Modify When executing on the command line having only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with your new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the command line to be tested...
Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...
Need C programming help. I've started to work on the program, however I struggle when using files and pointers. Any help is appreciated as I am having a hard time comleting this code. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE 100 #define MAX_NAME 30 int countLinesInFile(FILE* fPtr); int findPlayerByName(char** names, char* target, int size); int findMVP(int* goals, int* assists, int size); void printPlayers(int* goals, int* assists, char** names, int size); void allocateMemory(int** goals, int** assists, char*** names, int size);...
PLease explain output of these two programs: 1. #include <stdio.h> typedef struct { char *name; int x, y; int h, w; } box; typedef struct { unsigned int baud : 5; unsigned int div2 : 1; unsigned int use_external_clock : 1; } flags; int main(int argc, char** argv){ printf("The size of box is %d bytes\n", sizeof(box)); printf("The size of flags is %d bytes\n", sizeof(flags)); return 0; } 2. #include <stdio.h> #include <string.h> /* define simple structure */ struct { unsigned...
C programming How to get rid of the below error. I do not know why is this happening and how can I correct it. Please help. typedef struct circleroute{ char* name; } Circle; Circle* circlemalloc(int nroutes); //error free Circle* readRoute(FILE* fin); //error free int main(int argc, char* argv[]){ if(argc < 2){ if(argv[1] != NULL){ FILE *fin = fopen(argv[1], "r"); if(fin == NULL){ printf("Unable to open file %s!\n", argv[1]); return EXIT_FAILURE; } int routes; fscanf(fin, "%d", &routes); Circle* c1= circlemalloc(routes); for(int...
Given the following program: #include <stdio.h> struct student { int id; char name[20]; char grade; }; void func(struct student stud); int main() { struct student astud; astud.id=9401; strcpy(astud.name, "Joe"); astud.grade = 'A'; func(astud); return 0; } Abdelghani Bellaachia, CSCI 1121 Page: 16 void func(struct student astud) { printf(" Id is: %d \n", astud.id); printf(" Name is: %s \n", astud.name); printf(" Grade is: %c \n", astud.grade); } Modify this program to include the address of a student as a separate structure....