How to get input from bash terminal into my program - c

One of my assignments has the following question:
One function (function #2) of your program is to read in the content of an instance file. To read in
the file "
instance10
001.txt
" you will execute the command:
NameOfProgram -i instance10
001.txt
Here "
-i " is the command-line option that indicates the succeeding argument is the
input filename.
This is what I have done so far, mostly a skeleton:
/* Assignment 1 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
FILE *fp;
int max_x, max_y, num_pt, rand_inst;
int *x_coordinate, *y_coordinate;
int inputfile = 0, outputfile = 0;
int i;
if (argc == 1)
{
/* to generate random instances, accepting parameters from stdin */
printf("Generating random instances...");
printf("Enter the circuit board size MAX_X MAX_Y: ");
scanf("%d %d", &max_x, &max_y);
printf("Enter the number of points NUM_PT: ");
scanf("%d", &num_pt);
printf("Enter the number of random instances to be generated: ");
scanf("%d", &rand_inst);
return 1;
}
for (i = 1; i < argc; i++)
{
if (strcmp (argv[i], "-i") == 0)
inputfile = i+1;
else if (strcmp (argv[i], "-o") == 0)
outputfile = i+1;
}
if (inputfile == 0)
{
/* invalid comman line options */
printf("\nIncorrect command-line\n");
printf("myprogram [-i inputfile [-o outputfile]]");
return -1;
}
**/* THIS IS WHERE I NEED HELP */**
if (inputfile == 1)
{
fp = fopen(/*Name of the input file (instance10_001.txt) */, "r")
}
if ((fp = fopen(argv[inputfile], "r")) == NULL)
{
/* open file error */
return -2;
}
while (fscanf(fp, "%d", &max_x) != 1)
{
if (ferror(fp))
{
/* read error */
fclose(fp);
return -3;
}
if (feof(fp))
{
/* no integer to read */
fclose(fp);
return -4;
}
fscanf(, "%*[^\n]"); /*skip the rest of line */
}
if (fscanf(fp, "%d", &max_y) != 1)
{
/* max_y not following max_x */
fclose(fp);
return -5;
}
while (fscanf(fp, "%d", &num_pt) != 1)
{
if(ferror(fp))
{
/* read error */
fclose(fp);
return -6;
}
if (feof(fp))
{
/* no integer to read */
fclose(fp);
return -7;
}
fscanf(fp, "%*[^\n]"); /* skip the rest of line */
}
x_coordinate = (int *)malloc(num_pt * sizeof(int));
y_coordinate = (int *)malloc(num_pt * sizeof(int));
for (i = 0; i < num_pt; i++)
{
while (fscanf(fp, "%d", &x_coordinate[i]) != 1)
{
if (ferror(fp))
{
/* read error */
fclose(fp);
return -8;
}
if (feof(fp))
{
/* no integer to read */
fclose(fp);
return -9;
}
fscanf(fp, "%*[^\n]"); /* skip the rest of line */
}
if (fscanf(fp, "%d", &y_coordinate[i]) != 1)
{
/* y_coordinate not following x_coordinate */
fclose(fp);
return -10;
}
}
fclose(fp);
if (outputfile > 0)
{
if ((fp = fopen(argv[outputfile], "w")) == NULL)
{
/* open file error */
return -2;
}
fprintf(fp, "##################################################\n");
fprintf(fp, "#%s\n", argv[inputfile]);
fprintf(fp, "#area [0, MAX_X] x [0, MAX_Y]\n");
fprintf(fp, "%d\t%d\n", max_x, max_y);
fprintf(fp, "#number of points NUM_PT\n");
fprintf(fp, "%d\n", num_pt);
fprintf(fp, "#coordinates\n");
for (i = 0; i < num_pt; i++)
{
fprintf(fp, "%d\t%d\n", x_coordinate[i], y_coordinate[i]);
}
fprintf(fp, "#end of instance\n");
fclose(fp);
}
else
{
printf("##################################################\n");
printf("#%s\n", argv[inputfile]);
printf("#area [0, MAX_X] x [0, MAX_Y]\n");
printf("%d\t%d\n", max_x, max_y);
printf("#number of points NUM_PT\n");
printf("%d\n", num_pt);
printf("#coordinates\n");
for (i = 0; i < num_pt; i++)
{
printf("%d\t%d\n", x_coordinate[i], y_coordinate[i]);
}
printf("#end of instance\n");
}
free(x_coordinate);
free(y_coordinate);
return 0;
}
I am wondering how I can read the name of the input file from bash terminal. Should I use scanf?
how do I get what the user has inputted as the input file? Like for example if the user is running my program from bash with ./myprogram -i instance10_001.txt, how can I open the inputted file in my program?
PS I am using my Ubuntu terminal to access my lab computer via ssh.
Language: c99 ; Compiler: gcc

This looks like a simple error in your if statment. You're saying if, and only if, inputfile is 1 (which means -o must have been argv[0]) it will open inputfile.
if (inputfile == 0)
{
/* invalid command line options */
printf("\nIncorrect command-line\n");
printf("myprogram [-i inputfile [-o outputfile]]");
return -1;
}
else /* if inputfile is not equal to 0, then this will execute. */
{
fp = fopen(argv[inputfile], "r");
}
Also, there's another problem here, in which you assign fp to a function and then reopen the file already opened in fp:
/* removed fp = fopen (a function) */
if (fp == NULL) /* You already opened the file; no need to open again until fclose */
{
/* open file error */
return -2;
}
Also, in this block of code:
while (fscanf(fp, "%d", &x_coordinate[i]) != 1)
{
if (ferror(fp))
{
/* read error */
fclose(fp);
return -8;
}
if (feof(fp))
{
/* no integer to read */
fclose(fp);
return -9;
}
fscanf(fp, "%*[^\n]"); /* skip the rest of line */
}
Fscanf returns the number of arguments successfully filled, which in this case, will always be 1.
Note that similar problems may be present in the remainder of the code.

Related

How to read a file into an array of structs?

Im trying to read a text file into an array of structs, but when trying to print the array, the struct is empty. The printing function works fine and I think the problem is in getRawData.
struct student
{
char ID[MAXID + 1];
char f_name[FIRST_NAME_LENGTH + 1];
char s_name[LAST_NAME_LENGTH + 1];
int points[MAXROUNDS];
};
//main//
case 'W':
if(save(array, len) == 0);
{
printf("Data saved.\n");
}
break;
case 'O':
if(getRawData(array, len));
{
printf("File read.\n");
}
break;
int save(struct student *h, int num_students)
{
char name[20];
printf("Enter file name: " );
scanf("%s", name); // Read in filename
FILE *output = fopen(name, "w"); // open the file to write
if (!output) {
return -1; // error
}
for (int i = 0; i < num_students; ++i)
{
fprintf(output, "%s %s %s \n", h[i].f_name, h[i].s_name, h[i].ID);
for(int j = 0; j < MAXROUNDS; j++)
{
fprintf(output, "%d\n", h[i].points[j]);
}
printf("Information of student %s %s (%s) written into file %s\n", h[i].s_name, h[i].f_name, h[i].ID, name);
}
fclose(output); // close
return 0;
}
int getRawData(struct student *records)
{
int i;
int nmemb; // amount of structs
char name[20];
printf("Name of the file to be opened: \n");
scanf("%s", name);
FILE *outtput = fopen(name, "r");
int ch=0;
int lines=0;
if (outtput == NULL);
return 0;
lines++;
while(!feof(outtput))
{
ch = fgetc(outtput);
if(ch == '\n')
{
lines++;
}
}
nmemb = lines / 7;
for(i = 0; i < nmemb; i++) {
fscanf(outtput, "%s %s %s", records[i].f_name, records[i].s_name, records[i].ID);
for(int j = 0; j < MAXROUNDS; j++)
{
fscanf(outtput, "%d\n", &records[i].points[j]);
}
}
printf("%d", lines);
return i;
}
So my goal is to get the data from the file and write it over whatever there is stored in the struct array. I would appreciate some help as I have been working on this for way too long.
Look at this code in getRawData(), first you are reading file to identify total number of lines:
while(!feof(outtput))
{
ch = fgetc(outtput);
if(ch == '\n')
.....
.....
due to this the file stream pointer pointing to EOF and after this, in the for loop, you are doing:
for(i = 0; i < nmemb; i++) {
fscanf(outtput, "%s %s %s", records[i].f_name, records[i].s_name, records[i].ID);
.....
.....
Here, the fscanf() must be returning the EOF because there is nothing remain to read from stream file. You should check the return value of fscanf() while reading file.
You should reset the pointer to start of file before reading it again. You can use either rewind(ptr) or fseek(fptr, 0, SEEK_SET). Below is a sample program to show you what is happening in your code and how the solution works:
#include <stdio.h>
int main (void) {
int ch;
int lines = 0;
char str[100];
FILE *fptr = fopen ("file.txt", "r");
if (fptr == NULL) {
fprintf (stderr, "Failed to open file");
return -1;
}
while (!feof(fptr)) {
ch = fgetc (fptr);
if(ch == '\n') {
lines++;
}
}
printf ("Number of lines in file: %d\n", lines);
printf ("ch : %d\n", ch);
printf ("Now try to read file using fscanf()\n");
ch = fscanf (fptr, "%s", str);
printf ("fscanf() return value, ch : %d\n", ch);
printf ("Resetting the file pointer to the start of file\n");
rewind (fptr); // This will reset the pointer to the start of file
printf ("Reading file..\n");
while ((ch = fscanf (fptr, "%s", str)) == 1) {
printf ("%s", str);
}
printf ("\nch : %d\n", ch);
fclose (fptr);
return 0;
}
The content of file reading in the above program:
Hello Vilho..
How are you!
Output:
Number of lines in file: 2
ch : -1
Now try to read file using fscanf()
fscanf() return value, ch : -1
Resetting the file pointer to the start of file
Reading file..
HelloVilho..Howareyou!
ch : -1
Here you can see, the first ch : -1 indicate that the file pointer is at EOF and if you try to read you will get EOF because there is nothing left to read. After resetting file pointer, you can see fscanf() is able to read file.
You should not use while (!feof(file)). Check this.

open file in C and I don't know how do I get the width and height of the file

This is my code to open any file from the console.
But, I don't know how I get the width and height of the file automatically.
#include <stdio.h>
int main(int argc, char *argv[]){
char dat;
FILE *fs;
fs = fopen(argv[1], "r");
while((dat = fgetc(fs))!=EOF){
printf("%c",dat);
}
fclose(fs);
printf("\n");
return 0;
}
With width and high you mean the number of rows and columns of the file?
In this case you can check the newline character:
int rows = 0;
int cols = 0;
int rowcols = 0;
while ((dat = fgetc(fs)) != EOF) {
if (dat == '\n') {
if (rowcols > cols) {
cols = rowcols;
}
rowcols = 0;
rows++;
} else {
if (dat != '\r') { /* Do not count the carriage return */
rowcols++;
}
}
printf("%c",dat);
}
/* If the file does not end with a newline */
if (rowcols != 0) {
if (rowcols > cols) {
cols = rowcols;
}
rows++;
}
printf("Rows = %d, Cols = %d\n", rows, cols);
On the other hand, always check the number of arguments passed to main and the result of fopen:
if (argc < 2) {
fprintf(stderr, "Usage = %s file\n", argv[0]);
exit(EXIT_FAILURE);
}
...
fs = fopen(argv[1], "r");
if (fs == NULL) {
perror("fopen");
exit(EXIT_FAILURE);
}
Here is the sample code. explanation I wrote in comments
int main(int argc, char *argv[]){
FILE *fs;
fs = fopen(argv[1], "r");
/* always check return value of fopen() */
if(fs == NULL) {
/* put some mesage */
return 0;
}
/* width width means which lines having max no of characters */
/* step-1 : use fgets() bcz it read line by line */
char buf[100];/* assuming each line have less than 100 char */
int width = 0,len = 0,height = 0;
while(fgets(buf,sizeof(buf),fs)) {
/* now buf having one line, your job is find the length of each line
and do comparison mechanism */
len = strlen(buf);
if(width < len) {
width = len;
}
height++;/* its height bcz when above loop fails
height value is nothing but no of line in file*/
}
printf("width = %d and height = %d \n",width,height );
fclose(fs);
return 0;
}
To know how fgets() works. open man 3 fgets from command line & check.

C Programming - Difficulty with Parsing a Temp Folder

I'm having some trouble with parsing a temp.txt folder which contains the basenames of various files in a directory. I'd like to sort through this file, figure out what each of the file types are line by line, and then delete them from the temp file. I have been able to do carry out the parsing of the file names to discover their type, but when I call my delete function... it only works for some of the files and leaves behind some junk occasionally.
The input file looks like this:
temp.txt input
The output file after running looks like this:
temp.txt output
#include < stdio.h >
#include < string.h >
#include < unistd.h >
#include < stdlib.h >
/* This program module parses through the temp.txt file and finds .gif, .png, and .bmp files
and prints to standard output their file type in the order in which they're found.
It also takes all the files that aren't of those three types and puts them in a junk file.
*/
int deleteline(int delete_line);
int main(int argc, char * argv[]) {
FILE * file = fopen("temp.txt", "r"); /* should check the result */
FILE * myfile = fopen("junkfiles.txt", "w");
char line[4069];
char * gif = ".gif";
char * png = ".png";
char * bmp = ".bmp";
int i = 0;
int j = 0;
// if tempfile cannot be created, error handle
if (!file) {
puts("Some kind of file error!");
return 1;
}
// if junkfile cannot be created, error handle
if (!myfile) {
puts("Some kind of file error!");
return 1;
}
while (fgets(line, sizeof(line), file)) {
i++;
if (strstr(line, gif) != NULL) {
j = deleteline(i);
fflush(NULL);
if (j == 0) {
printf("File on line %d is: ", i);
puts(line);
printf("This is a .gif\n\n");
} else {
printf("Some error on line %d\n", i);
}
} else if (strstr(line, png) != NULL) {
j = deleteline(i);
if (j == 0) {
printf("File on line %d is: ", i);
puts(line);
printf("This is a .png\n\n");
} else {
printf("Some error on line %d\n", i);
}
} else if (strstr(line, bmp) != NULL) {
j = deleteline(i);
if (j == 0) {
printf("File on line %d is: ", i);
puts(line);
printf("This is a .bmp\n\n");
} else {
printf("Some error on line %d\n", i);
}
} else {
j = deleteline(i);
if (j == 0) {
printf("The file on line %d is junk.\n\n", i);
} else {
printf("Some error on line %d\n\n", i);
}
fprintf(myfile, "%s", line);
}
}
/* may check feof here to make a difference between eof and io failure -- network
timeout for instance */
fclose(file);
fclose(myfile);
fflush(NULL);
return 0;
}
int deleteline(int delete_line) {
FILE * fileptr1, * fileptr2;
char * filename = "temp.txt";
char ch;
int temp = 1;
//open file in read mode
fileptr1 = fopen(filename, "r");
ch = getc(fileptr1);
//rewind
rewind(fileptr1);
//open new file in write mode
fileptr2 = fopen("replica.txt", "w");
while (ch != EOF) {
ch = getc(fileptr1);
if (ch == '\n')
temp++;
//except the line to be deleted
if (temp != delete_line) {
//copy all lines in file replica.c
putc(ch, fileptr2);
}
}
fclose(fileptr1);
fclose(fileptr2);
remove(filename);
//rename the file replica.c to original name
rename("replica.txt", filename);
return 0;
}

how to enter a function to a file in c

I have this function in a code that im trying to enter to an I/O file and I cannot seem to do it.
void show_list(int whyeven[stuff], char *hatred[stuff])
{
for (int g = 0; g < stuff - 1; g++)
{
if (whyeven[g] < 10 || whyeven[g] == 0)
{
printf("%s - %d (*) you should buy more of this stuff\n\n",hatred[g], whyeven[g]);
}
else if (whyeven[g] > 10)
{
printf("%s - %d\n\n", hatred[g], whyeven[g]);
}
}
}
int main()
{
show_list(moarstuff, items);
return 0;
}
printf() prints to stdout. You need to fopen() that file and then use fprintf() with the returned from fopen() FILE* pointer as the first argument.
/* Open the file for writing */
FILE* fp = fopen("filename.txt", "w");
/* Check for errors */
if (fp == NULL)
{
/* Notify the user of the respective error and exit */
fprintf(stderr, "%s\n", strerror(errno));
exit(1);
}
/* Write to the file */
fprintf(fp, "Hello!\n");
/* Close the file */
fclose(fp);
Note: Your question was quite unclear and this answer is based on what I could understand out of it.

C using a file as an argument

The problem I am having is that whenever I run the code I get the Null value for myfile before I even get to pass the file name as the argument into the program and i'm not sure why any help is much appreciated.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE* myFile; // file pointer
myFile = fopen(argv[1] , "r"); //open file
if(myFile==NULL)
{
printf("Can't Open FIle.\n"); // if file doesn't exist then exit
exit(0);
}
int A[10000]={0};
int B[10000]={0};
double C[10000]={0};
int N,M;
int i=0;
fscanf (myFile, "%d", &N); //input N from file
printf("%d\n",N);
if(N>100)
{
exit(0); // if N>100 then exit
}
while (!feof (myFile)) // loop until file pointer reaches to the end of file
{
fscanf (myFile, "%d", &A[i]); //input source
fscanf (myFile, "%d", &B[i]); // input destination
fscanf (myFile, "%lf", &C[i]); // input time
i++;
}
fclose (myFile); //close file
M=i; // number of lines = M
for (i = 0; i < M; i++)
{
if(A[i]==0) //end of output
break;
else
{
printf("%d %d %lf:\n",A[i],B[i],C[i]); //print source, destination and time
if(A[i]>=1&&A[i]<=N)
{
if(B[i]>=1&&B[i]<=N)
{
if(A[i]==B[i])
{
printf("Error:Source city is the same as destination city.\n"); //same source and destination error:condition
}
else
{
if(C[i]<0)
{
printf("Error:Invalid Time.\n"); //invalid time
}
else
{
//
}
}
}
else
{
printf("Error: Invalid destination city.\n"); //invalid destination condition
}
}
else
{
printf("Error: Invalid source city.\n"); //invalid source condition
}
}
}
return 0;
}
If you don't pass a file name as an argument to the program, the contents of argv[1] are undefined.
Since anything could be there in argv[1] when the program attempts to open the file, the most probable thing is that there is no file with that name, and hence fopen() returns NULL.
You should not try to access command line arguments without checking that they were supplied, for that check argc, if you just expect 1 argument, then a simple check would be
char filename[256]; /* the size should be reasonable
* can be PATH_MAX or MAX_PATH,
* depending on whether it's Windows
* or not
*/
if (argc < 2)
{
size_t length;
fprintf(stdout, "Error: %s program expects one argument\n", argv[0]);
fprintf(stdout, "Please input the file name: ");
if (fgets(filename, sizeof(filename), stdin) == NULL)
{
fprintf(stdout, "Error: unexpected error\n");
return -1;
}
length = strlen(filename);
if (length == 0)
{
fprintf(stdout, "Error: the provided file name is invalid.\n");
return -1;
}
if (filename[length - 1] == '\n')
filename[length - 1] = 0;
}
else
strcpy(filename, argv[1]);

Resources