every thing works till the fopen, the sprintf works as i'm printing to check that. i just doing know what is causing this not to work

it prints "cannot open (null)" but the filename var does have something assigned to it.

any help will be welcomed

char tmpfilename[10];
	char filename[15];

	if(argv[2] == NULL){
		printf("no output file name specified\n");
		printf("would you like to specify one now? (y/n)\n");
		scanf("%c", &a);
		if(a=='y'){
			printf("enter the name of the file to be outputted (less than 10 letters)\n");
			scanf("%s", &tmpfilename);
			sprintf(filename,"%s.ps",tmpfilename);
			printf("%s there should be a string here\n",filename);
		
		--->	if(!(out = fopen(filename, "w"))){
				fprintf(stderr,"Cannot open %s\n",filename);
				exit(2);
			}
		}
		if(a=='n'){
				printf("ok, exiting\n");
		exit(1);
		}
		
		if(a != 'y' && a != 'n'){
			printf("unexpected input...exiting\n");
			exit(1);
		}
	}

Recommended Answers

All 6 Replies

Post some more code, for example all the other declarations of variables used in this code.

there is only 2 var missing and thats:

char a;
FILE *out;

argv[2] is NULL which is why this bit of code comes into play

there is only 2 var missing and thats:

char a;
FILE *out;

argv[2] is NULL which is why this bit of code comes into play

On the phone
Nick: Doctor, can you cure my cough? It is a horrible cough, I cannot sleep at night.
Dr. Hell: Sorry, Nick, I cannot prescribe the proper medication without seeing you first, make an appointment for tomorrow.
Nick: OK.
Next day at Dr Hell's consult.
Dr Hell: Who are you?
Nick's buddy: I am Nick's best friend. He told me that you needed to see him first, in order for you to prescribe medicine for his cough. So he sent me with his best photograph taken last year at the summer's party.


EvilOrange> there is only 2 var missing and thats:
That's not enough.

Dr Hell guesses that it has to do with...

scanf("%s", &tmpfilename);
commented: My diagnosis - this code is dead, it has ceased to be +36

ok here his the print out i get from terminal:

>./run final.txt
>no output file name specified                       <--Line 20
>would you like to specify one now? (y/n)      <--Line 21
>y                                                               <--Line 22
>enter the name of the file to be outputted (less than 10 letters)  <--Line 24
>hds                                                            <--Line 25
>hds.ps <-there should be a string here             <--Line 27 - proving the scanf("%s", &tmpfilename) is working
>Cannot open (null)                                      <--Line 30

and here is the the totality of the file input code:

int main(int argc, char *argv[]){
	int i;
	char a;
	char tmpfilename[SIZE];
	char filename[15];
	

	FILE *fp, *out;
	Program prog;
	


	if(!(fp = fopen(argv[1], "r"))){
		fprintf(stderr,"Cannot open %s\n",argv[1]);
		exit(2);
	}

	
	if(argv[2] == NULL){
		printf("no output file name specified\n");
		printf("would you like to specify one now? (y/n)\n");
		scanf("%c", &a);
		if(a=='y'){
			printf("enter the name of the file to be outputted (less than 10 letters)\n");
			scanf("%s", &tmpfilename);
			sprintf(filename,"%s.ps",tmpfilename);
			printf("%s there should be a string here\n",filename);
		
			if(!(out = fopen(filename, "w"))){
				fprintf(stderr,"Cannot open %s\n",filename);
				exit(2);
			}
		}
		if(a=='n'){
				printf("ok, exiting\n");
		exit(1);
		}
		
		if(a != 'y' && a != 'n'){
			printf("unexpected input...exiting\n");
			exit(1);
		}
	}
	
	if(!(out = fopen(argv[2], "w"))){
		fprintf(stderr,"Cannot open %s\n",argv[2]);
		exit(2);
	}

EvilOrange> <--Line 27 - proving the scanf("%s", &tmpfilename) is working
Dealing with the format %s in scanf() you must pass the argument as plain tmpfilename or as &tmpfilename[0] , but not as &tmpfilename EvilOrange> >Cannot open (null) <--Line 30
Are you sure is line 30 and not line 46? fprintf(stderr,"Cannot open %s\n",argv[2]);

EvilOrange> <--Line 27 - proving the scanf("%s", &tmpfilename) is working
Dealing with the format %s in scanf() you must pass the argument as plain tmpfilename or as &tmpfilename[0] , but not as &tmpfilename EvilOrange> >Cannot open (null) <--Line 30
Are you sure is line 30 and not line 46? fprintf(stderr,"Cannot open %s\n",argv[2]);

bit embarrased now... had code duplication early on, and deleted the wrong snippet which is shown on 46, and you where correct about the cannot open (null)

it is a sunday and i shouldn't be working after all, thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.