Thread: Cannot open file with extension as .dat.xml

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    5

    Cannot open file with extension as .dat.xml

    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.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    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.?

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    5
    Quote Originally Posted by c99tutorial View Post
    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.?
    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;
    }
    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.

    Thank you

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by tewari2312 View Post
    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.
    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.

    Code:
    char fileread[] = "foo.dat.xml";
    FILE *Rfile = fopen(fileread, "r");
    if (!Rfile) {
        perror(fileread);
        exit(1);
    }
    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.

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    5
    "0444450.dat.xml: No such file or directory". This error message pops up. But the file is in the folder as can be seen.
    Cannot open file with extension as .dat.xml-2014_10_29_13_42_36_bin-jpg

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    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.

  7. #7
    Registered User
    Join Date
    Oct 2014
    Posts
    5
    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 ?

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Quote Originally Posted by tewari2312 View Post
    Can you also suggest any c compilers that will do the job ?
    What O/S (Operating System) are you running on? What is your current compiler? Version?

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    You could look at this article on the web site

    How to Get Started with C or C++ - Cprogramming.com

  10. #10
    Registered User
    Join Date
    Oct 2014
    Posts
    5
    My OS is windows-XP 64bit. Currently I am running Turbo C++ 4.0 on DOSbox.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a File Extension?
    By Nebbuchadnezzar in forum C++ Programming
    Replies: 3
    Last Post: 09-27-2006, 12:45 PM
  2. .ef file extension
    By xddxogm3 in forum Tech Board
    Replies: 8
    Last Post: 02-08-2006, 06:20 AM
  3. What file extension should I use?
    By f0r3nsic in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2005, 01:13 PM
  4. allow another file extension??
    By C+noob in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-03-2005, 08:52 PM
  5. get file extension
    By Nada in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 09:12 AM

Tags for this Thread