Thread: A strange problem with creating a file

  1. #1
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001

    A strange problem with creating a file

    I have been working on this program for some time now, and i've stripped it down into smaller executables so i can test each individual part of my code (a lot of debugging to do)...anyhow, i have a part of it worked out finally, it works, sort of. It is supposed to create a file name logically by concatenating two c style strings...that all works, but the final output it strange...code like this:
    Code:
    #include <iostream>
    #include <fstream>
    #include <stdio>
    using namespace std;
    
    char id[5];
    char fileHandle[]=".xml";
    char* fileName;
    
    
    int main() {
    
    
    cout<<"enter id"<<endl;
    cin>>id;
    
    fileName=strcat(id,fileHandle);
    //now open the file and create the fout object for writing
    ofstream fout(fileName);
    fout<<"ok";
    fout.close();
    
    
    return 0;
    }
    when i run the program, it creates a file with the correct data in it, however the file extension is messed up, the last letter of the file extension (in this case being 'L') is replaces with '1/4A' i've tried with different file extensions and the same thing happens....does anyone know whats happening here? i thought perhaps it has something to do with the terminating character of the string...but i don't know. thanks
    PHP and XML
    Let's talk about SAX

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > does anyone know whats happening here?
    Your arrays are too small

    char id[5];
    char fileHandle[]=".xml";

    ".xml" takes 5 chars before you do anything, so without any input, it will only just fit when you do the strcat

    Say
    char id[100];

    Your use of strcat is unusual (though not illegal), but perhaps gives the wrong impression that something new is being created.

  3. #3
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    well thanks anyway, i tried it but it didn't work....this time the file was called .xm@1/2A ...very strange, all the source code was the same besides adding the size 100 to the c string fileHandle....
    PHP and XML
    Let's talk about SAX

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Read again

    I didn't say FileHandle was 100 chars,

    I said
    id[100];

  5. #5
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    oh sorry, i feel stupid...anyhow i see why you make id so big, thanks for the help.
    PHP and XML
    Let's talk about SAX

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating File Handling Functions
    By td4nos in forum C Programming
    Replies: 6
    Last Post: 06-26-2009, 11:43 AM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Problem with file and array
    By paok in forum C Programming
    Replies: 5
    Last Post: 05-01-2008, 04:19 AM
  4. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 09:36 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM