I'm trying to write a program that will read the first character in a text file. If I use ./a.out myfile.txt it works as intended but if I use ./a.out <myfile.txt I get Segmentation fault: 11. The reason why I'm trying to include the <is because this what is in the spec of the assignment. The below code is just a simplified example that i've made that has the same issue:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int func(int argc, char **argv){
FILE *fp;
int test = 0;
fp = fopen(argv[1], "r");
fscanf(fp, "%i", &test);
printf( "current file: %s \n", argv[1]);
}
int main(int argc, char **argv){
func(argc, argv);
}
Is there any way I can get it to accept the argument as <myfile.txt?
No, nor should you try. Files redirected this way will appear at stdin and you should use that instead (hint: check argc).
If you want to use a file if specified, but otherwise stdin, use something like:
if (argc > 1)
fp = fopen(argv[1], "r");
else
fp = stdin;
In your command ./a.out <myfile you redirect stdin to myfile. This means reading from stdin is actually reading from myfile. So, in this case your argc == 1, so argv[1] you use to open is NULL (see main spec on its arguments). fopen crashes when uses NULL name.
You may do your utility in another way: always read stdin. When you need file to input do like this: cat myfile | ./a.out. This is very nice approach and worth considering.
Related
I am trying to write "File opened" in a existing test.txt file with C program. But I must not create a test.txt file with the program. I have to write with in a existing file.
I tried but can't.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
int main() {
char sentence[1000] = "File opened";
fopen("test.txt", "w");
fprintf("%s", sentence);
fclose();
return 0;
}
The error showing me is:
error: too few arguments to function 'fclose'
How can I do that? Please help me.
In future recommend that you do some searching for either existing questions that people have asked or some research. ie look up the manual or spec for fopen: https://man7.org/linux/man-pages/man3/fopen.3.html
Especially since your code doesn't compiled as it has errors, with the fopen, fprintf and fclose all missing arguments or assignment variables. Reading those errors and the compiler messages would have guided you to the solution. This is what it would look like with those fixed and the "w+" option used:
#include <stdio.h>
#include <stdlib.h>
int main() {
char sentence[1000] = "File opened";
FILE *file = fopen("test.txt", "w+");
fprintf( file, "%s", sentence);
fclose( file );
return 0;
}
This is a good tutorial if you want to know more: https://www.geeksforgeeks.org/basics-file-handling-c/
So i trying to input multiple files in the assigned program.
My input file code is as such:
int read_File(int *hp, int *d, int *s, char t[])
{
FILE *infile;
infile = fopen("input1.txt", "r");
if (!infile)
{
return 0;
}
else
{
fscanf(infile, "%d", hp);
fscanf(infile, "%d", d);
fscanf(infile, "%d", s);
fscanf(infile, "%s", t);
fclose(infile);
return 1;
}
i did
$>gcc assignedProgram.c -o nqt
$>./nqt input1.txt
but if i want to read input2.txt, i have to change from input1.txt to input2.txt in the codes. Is there anyway to bypass that and read input2.txt without changing from input1.txt to input2.txt in the codes
like when i tried ./nqt input1.txt => it's normal
BUT ./nqt input2.txt it's segmentation fault:11
I tried:
to change "input1.txt" in the codes to "nqt" but that was a dumb idea
and BTW: what is ./nqt
Please help me!
You must write main() as one of the equivalent forms below
int main(int argc, char **argv) { /*...*/ }
int main(int argc, char *argv[]) { /*...*/ }
so that argc and argv are set up by your environment to proper values.
For example
$ ./nqt input.txt
^^^^^ ^^^^^^^^^ --> 1
\\\\\-------------> 0
Translates in your program to
argc == 2
argv[0] ==> pointer to "./nqt"
argv[1] ==> pointer to "input.txt"
argv[2] ==> NULL
When it comes to nqt, it is the name of the program you specified with -o flag when compiling: gcc assignedProgram.c -o nqt. In order to run the program, you need to use ./ prefix, thus ./nqt means "run the program called nqt".
If you want to pass the name of the file as an argument, you should tell main function to accept command line arguments: int argc and char* argv[] (you can read about it here). Then the name of the file you will pass by running ./nqt <filename> would be stored in argv[1], which you should pass to read_File function as an argument.
Well, I am learning programming in C, and I got an assignment to get 3 characters from an input text file into 3 variables and then print their ASCII values.
I wrote this code:
#include <stdio.h>
int main()
{
char a,b,c;
printf("Insert 3 characters:\n");
a=getch();
b=getch();
c=getch();
printf("%d, %d, %d",(int)a,(int)b,(int)c);
}
I opened a text file (input.txt) and wrote there: "abc".
I managed to compile the code with the MinGW compiler, and on the CMD window that I opened in the folder of the .exe file, I wrote: "Task.exe <input.txt".
The program ran normally. I mean, it waited for me to input 3 characters.
What have I done wrong in my work?
help me please :)
You are asked to read from an input text file.
Why don't you use fopen to open a file handle, and fgetc to read from it?
You could perhaps use fscanf. Don't forget to use the resulting count.
And of course, you should call fclose. Using perror is useful to handle error cases.
So start your code with something that checks that your program has an argument, then fopen it:
int main(int argc, char**argv) {
if (argc<2) { fprintf(stderr, "missing program argument\n");
exit(EXIT_FAILURE); };
FILE* fil = fopen(argv[1], "r");
if (!fil) { perror(argv[1]); exit(EXIT_FAILURE); };
Then run Task.exe input.txt in your console (no redirection needed!).
You should take the habit of reading the documentation of every function you are using, of testing failure cases, of compiling with all warnings & debug info (gcc -Wall -Wextra -std=c99 -g), and of using the debugger (gdb).
I have a written a C program using Visual Studio 2008. The program compares to files in binary mode and tells us if the files are same or different.
I need to execute this program on command line and need to pass 2 arguments along with it.
the first argument is for the file to be compared and 2nd is the file to which it will be compared.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv){
int result_code;
char command_line[256];
sprintf(command_line, "FC /B %s %s > NUL:", argv[1], argv[2]);
result_code=system(command_line);
printf("%s file.\n", result_code ? "different" : "same");
return 0;
}
See this.
http://www.cprogramming.com/tutorial/print/lesson14.html
you can get plenty more from google.
So I'm trying to get the C program to read a filename from the command line in the following format:
cat (filename path) | (program name)
i can get it to read the name of the input file when its entered as a command line argument, but it won't read from the concatenated argument
here's the code, right now its reading the name of the file as if written after the program name on the command line.
#include <stdio.h>
#include <string.h>
//initialize file pointer
FILE *file;
//initialize global variables
#define DEFAULT_LEN 70
//main
int main(int argv, char *argc[]){
//open File for reading
file = fopen (argc[1],"r");
//test for failure of file open, if failed, print message, quit
if(file == NULL){
printf("I'm sorry Dave, I'm araid I can't do that.\n");
printf("Open the file \"%s\" that is.\n", argc[1]);
return(0);
}
//read the first line of a file into an array
char temp[DEFAULT_LEN]; //where the read data is put
fgets(temp,DEFAULT_LEN,file); //stops at DEFAULT_LEN on \n
//print out temp
printf("%s\n", temp);
//close file, return 0 for main
fclose(file);
return(0);
}
any help would be appreciated
The reason your program can't get the file name is because you're not giving it to it.
If you run your program as:
prog hello.txt
it's given the argument hello.txt in argc/argv.
However, what you're doing is:
cat hello.txt | prog
which means the shell is opening the file and feeding it into the standard input of your program. Actually, to be more accurate, cat is opening the file and the shell is simply connecting the standard output of cat to the standard input of prog.
One way around this is to check the number of arguments (argc is usually the count, argv[] the values, despite the roundabout way you have it in your code) and, if it's zero, argc == 1, read your file from standard input.
Only if an argument is given do you open that file and read it. That's the way a lot of UNIX utilities work:
od -xcb hello.txt # will dump the file.
cat hello.txt | od -xcb # will dump the file, but using standard input.
echo hello | od -xcb # will dump your "hello" string.
Some even change their behaviour depending on how they're invoked, wc being one example - it shows the file name(s) if it knows them:
pax> wc -l qq.c
29 qq.c
pax> cat qq.c | wc -l
29
pax> wc -l *.c
0 a b.c
168 binmath.c
49 qq-save.c
29 qq.c
11 qq2.c
5 qqq.c
18 xx.c
280 total
pax> cat *.c | wc -l
280
Note that last case - because all files are being presented over the single standard input stream, there's no way to tell how many files there are. wc will just tally up the entire contents of that stream.
Try this instead:
#include <stdio.h>
#include <string.h>
#define DEFAULT_LEN 70
int main (int argc, char *argv[]) {
FILE *file;
// Either select standard input or open the given file.
if (argc == 1) {
file = stdin;
} else {
file = fopen (argv[1], "r");
if (file == NULL) {
printf ("I'm sorry Dave, I can't do that\n");
printf (" (open the file '%s', that is).\n", argv[1]);
return 1;
}
}
// Now you're connected to stdin or the file itself, no
// difference in handling them (unless you do weird fseek
// sort of stuff).
char temp[DEFAULT_LEN];
fgets (temp, DEFAULT_LEN, file);
printf ("%s\n", temp);
// Only close file if you opened it yourself.
if (argc != 1)
fclose (file);
return 0;
}
This allows you to use both the file and standard input method as follows:
pax> prog prog.c
#include <stdio.h>
pax> echo hello | prog
hello
Piping content into a process does not put the values into argv; rather, it puts the values onto that process's stdin.
You need to check if argc is greater than 1. If it is, then argv[1] has the filename you've been given (well, the first argument to the program, anyway). If not, you need to read from stdin to get the filename.
To read from the contantenated, you need to read from STDIN
char c = (char)fgetc(stdin)