input arguements

Hi,I am trying to take two input arguments (basically names of two files).I am concatenating each of them with their complete directory path and using the resuting strings as the filename for fopen.However, It isnt working.
pls help..
Below is part of my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<stdio.h>
#include<conio.h>                           //test.cpp
#include<string.h>
#include<ctype.h>                        
int main(int argc, char **argv)
{
char* substring(char*,char*,int,int); 
FILE *f1;
FILE *f2;
char fa;
char fb;
char a[500],b[500],temp[500];
int flag=0;int count1=0;
int i,j,k,l=0,tag1,tag2,count=0;
i=0;
j=0;
k=0;
fa='0';
fb='0';
char x[]="C:/Dev-C++/Bin/";
char y[]="C:/Dev-C++/Bin/";
strcat(x,argv[1]);
strcat(y,argv[2]);
f1=fopen(x,"rb");
f2=fopen(y,"rb");
if (f1==NULL)
{
   printf("\nDoes'nt exist\n");
}
.
.
.
return(0);
}

I get output as "Does'nt exist"
Last edited on
Please use the "#" to post UR code...
May be due to the mode in which U r trying to open the file...

1
2
f1=fopen(x,"rb");
f2=fopen(y,"rb");


As U r opening both the files in read mode "rb" so if the specified fiel does not present then it will not open the file.

If U want to create the file then use write mode "wb".

See whether it sorts out ur problem or not.
I am using files which are already created as i have to read them.
So I'll have to use "rb".
I just wrote a test program seperately and it seems that if i use two file pointers and use fopen() on both seperately , then i get a wrong output of "File Does'nt exist".
If I simply remove the other file pointer and remove the corresponding fopen() for it..then i get correct output.
Bottomline: Presence of 2 fopens() is causing the problem.

Pls help me find a solution.
Ok..i found the solution.
I put limits inside declaration of arrays x[] and y[] and thus changed it to
x[50] and y[50]..
Its working now.
Thanks for replying
Hi polo87,
Does the below code creates problem???

1
2
char x[]="C:/Dev-C++/Bin/";
char y[]="C:/Dev-C++/Bin/";


I think there is no problem in the above declaration, we can initialize the char array like above.


1
2
char x[]="C:/Dev-C++/Bin/";
char y[]="C:/Dev-C++/Bin/";



I think there is no problem in the above declaration, we can initialize the char array like above.


The initialization of this array is completely fine. However, you cannot append anything to it as you were trying to do. That results in undefined behavior. x and y will be initialized with a length equal to exactly the size of the string specified. There is no room in the array to append to it using strcat, which is why the program was not behaving correctly. Specifying an array length of 50, provided sufficient room in the character array to allow a concat operation in your specific case. The better solution would be to use a std::string, which can grow dynamically.

The presence of two separate file streams and two separate fopen operations had nothing to do with the original problem.
Topic archived. No new replies allowed.