No my main is bassically empty but it's not working with your code as well. Any idea what can be wrong?
No my main is bassically empty but it's not working with your code as well. Any idea what can be wrong?
Well what's your latest code?
And who are you talking to when you say "it doesn't work"?
From some of the things you've said, the first suggestion would be to stop using the C++ compiler to compile C code.
Perhaps begin with changing your source file from prog.cpp to prog.c
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Try this and tell us what the output is.
Code:#include <stdio.h> int main(int argc, char *argv[]) { int i = 0; printf("n\Argument count: %d", argc); printf("\nArguments:"); printf("\n----------"); for (; i < argc; ++i) printf("\nargv[%d] = %s", argc, argv[i]); return 0; }
My actual code looks like this
When I compile the code from OldGuy2 I receive:Code:#include <stdio.h> #include <stdlib.h> #include <math.h> int main() { char *filename; FILE *in; int i,q; int *reserve; int size; in = fopen(filename,"r"); if(in==NULL) { perror("error reading data"); //return 0; } for(i=0;;i++) { if(EOF==fscanf(in,"%i",&q)) break; } size = i; reserve = (int*)malloc(sizeof(int)* size); if (reserve==NULL)return 0; rewind(in); for(i=0;i<size;i++) fscanf(in,"%i",&reserve[i]); fclose(in); return 0; }
warning: unknown escape sequence: '\A'
printf("n\Argument count: %d", argc);
and when i run the program, I receive this:
nArgument count: 2
Arguments:
----------
argv[2] = test
argv[2] = 5.7k-5.7.dat
test is the name of the programm von OldGuy2 and 5.7k-5.7.dat is the file I try to read.
Perhaps you need to move the backslash
printf("n\Argument
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Yes it was a silly typo.Perhaps you need to move the backslash
printf("n\Argument
Have a closer look at line 8 and 14 - how can this work ?
Last edited by OldGuy2; 11-12-2017 at 10:33 AM.
lets recap:
you want to get the filename off the command line?
your code in post #19 needs an assignment to the var filename
to work.Code:filename = "myfile";
to get it off the command line without using your var filename needs to be like this.
your command line works on zero base counting. argc = 0, 1 , 2 , 3 , etc....Code:int main ( int argc, const char **argv) { if (argc < 2) { printf("no file present\n"); exit(1); } filename = argv[1]; // or in = fopen(argv[1],"r"); ..... more code here return 0; }
command line
./programName filename command_line _options
a good way to gain an understanding of this is. run this program
then just type stuff on your command line hit enter then see what happens. while running this program of course.Code:int main (int argc, const char **argv) { int i = 0; for ( i = 0; i < argc; i++) printf("argc = %d ; argv = %s\n", i , argv[i]); return 0 ; }
Last edited by userxbw; 11-12-2017 at 02:16 PM.
When i try to pass the filname in the code like userxbw told me, the programm can be compiled but when I try to run it, it never stops! I also tried your code userxbw and its working fine but we never learned about something written in the main() function in class so I would be very happy if this isn't necessary. it also seems like my anti virus program does not like my program.
OldGuy2 what is wrong with line 8:
A pointer from type char which later will be the name of my file.Code:char *filename