0

I have to split up a command line argument in order to determine the file type. In order to do this, I'm using

char fileName; //global variable, just below function prototypes

char *fileType;

fileType= strtok(inputFile, "."); //first string split
fileName= (int) &fileType; //store the file name for further use

fileType= strtok(NULL, "."); //get the file type

The tokenizer function is working, but in the interest of keeping redundant code to a minimum, I want to store the filename, as I will have to create a new file with the same name, but different extension later on.

In the debugger, the the fileName variable never gets used. Why?

2
  • 1
    Why are you casting the address of fileType to an integer?! Is it just for the compiler to stop complaining. Commented Feb 21, 2011 at 16:56
  • basically, thats it. I get an integer from pointer without cast warning otherwise. Commented Feb 21, 2011 at 16:59

2 Answers 2

1

About

char fileName; //global variable, just below function prototypes

if fileName is supposed to be a string then it must be a pointer to the first char in that string (i.e. char *fileName).

if fileName is suposed to be a pointer to a string, then it should be declared as char **fileName.


About

char *fileType;

if this is a local variable, and fileName is a pointer to it, then after the function return it will be destroyed and the pointer will point to unknown data.


About

fileName= (int) &fileType; //store the file name for further use

this seem non-sense to me. Why cast the address of fileType to an integer? I guess the compiler complained because fileName is a char and not a char * and you noticed this would fix the error. Don't do this kind of fixes without understanding what you're doing, because programming like that just leads to esoterical code which must probably won't work as intended anyway.

So, if fileName is defined as char * then just do fileName = fileType. Else, if fileName is declared as char ** then do fileName = &fileType;


About

fileType= strtok(NULL, "."); //get the file type

if strtok() can return a pointer to a new string, and fileName is declared as char *, then whatever you stored previously in it wouldn't be meaningful anymore. In this case fileName would need to be a char ** (a pointer to a string) which I proposed in my first comment.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to use a char array along with strcpy.

Like this:

#define MAX_FILENAME_SIZE 200
char fileName[MAX_FILENAME_SIZE];

int f()
{
    fileType = strtok(inputFule, ".");
    // then use strcpy
    strcpy(fileName, fileType);

}

A char global would only store one character.

5 Comments

It was the string copy addition that helped out. I was trying to assign the variable directly, without using the function. Is that still possible?
You can't just assign. You need to use strcpy in your case since the string returned by strtok needs to be copied into the heap. I would strongly recommend you use char fileName[200]. If it's working with just char fileName, it does not mean it will always work. REMEMBER: char fileName only guarantees you enough space for one char.
Understood. Shifting from Java to C is quite interesting, makes me feel like I'm back in Intro to Programming!
in that case, you may as well use strncpy and/or check the length of fileType, especially if inputFule is controlled by the user.
Downvoted for the strong recommendation to use bad programming practices such as arbitrary constant buffer sizes and strcpy. Do what BatchyX said instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.