Hi I am writing contents of one .xml file into another .xml file
I am able to access .xml files and write into another .xml file
however if i encounter .dat.xml the file fails to be read.
Hi I am writing contents of one .xml file into another .xml file
I am able to access .xml files and write into another .xml file
however if i encounter .dat.xml the file fails to be read.
The contents of the files is usually what matters, and not the filename. For example, is the file you're having trouble with written in the correct format? Maybe open it in a text editor and see if it looks similar to the files you can read.
Is this a programming problem? If so maybe you can give some more specifics on what you're doing. Are you trying to parse XML? What do you mean by "fail to read"? Did it fail to parse, fail to find the file, etc.?
I am just trying to read the file. No parsing is needed. If the file to be read is abc.xml the code works fine. If it is abc.dat.xml the file pointer Rfile becomes NULL.Code:#include<stdio.h>#include<conio.h> #include<stdlib.h> #include<string.h> int main(int argc,char *argv[]) { FILE *Rfile; FILE *Wfile; char line[500]; char fileread[300]; char filewrite[300]; strcpy(fileread,argv[1]); strcpy(filewrite,argv[2]); Rfile=fopen(fileread,"r"); Wfile=fopen(filewrite,"w"); if(Rfile!=NULL) { while(fgets(line,sizeof line,Rfile)!=NULL) { fputs(line,Wfile); } fclose(Rfile); fclose(Wfile); } return 0; }
Thank you
If you open with fopen and NULL is returned, that means an error occured and you should handle it somehow. The simplest way to handle this error is to use perror to print a human readable error message and then exit the program. For example, maybe you mistyped the filename or there is some other reason that it can't be opened. I doubt the the mere fact that a file is called abc.dat.xml is a reason that it can't be opened.
There are some other problems with your code, for example it is poorly formatted. Only one include should be a on a line, etc. Also you allocate buffers of 300 characters for filenames but you don't consider if a user of your program overflows this buffer.Code:char fileread[] = "foo.dat.xml"; FILE *Rfile = fopen(fileread, "r"); if (!Rfile) { perror(fileread); exit(1); }
"0444450.dat.xml: No such file or directory". This error message pops up. But the file is in the folder as can be seen.
I noticed you wrote "#include <conio.h>". This is sometimes used with deprecated DOS compilers. If you're compiling the program to a DOS executable and running it using the DOS emulation layer, then you must specify filenames using the DOS 8+3 format (aka short filenames), for example, the short name for "0444450.dat.xml" might be expresssed as 044445~1.XML. To be sure you would have to print a directory listing of that directory from within a DOS program to see what the short names are.
If you can open 044445~1.XML from within your C Program but you can't open 0444450.dat.xml, then I suggest you get an updated compiler ASAP, unless you are actually wanting to develop DOS applications.
It worked ! I am extremely thankful to you. I also had trouble creating NEW_0444450.dat.xml. Can you also suggest any c compilers that will do the job ?
You could look at this article on the web site
How to Get Started with C or C++ - Cprogramming.com
My OS is windows-XP 64bit. Currently I am running Turbo C++ 4.0 on DOSbox.