Welcome! Log In Create A New Profile

Advanced

Problem with fopen

Posted by SpaceJump 
Problem with fopen
November 23, 2008 02:53PM
I have a problem with opening a file on sd with fopen.

If I do it like this, it works:
FILE* inputFile;
inputFile = fopen("/config.txt", "rb");

This however doesn't work:
char *path = "/config.txt";
FILE* inputFile;
inputFile = fopen(path, "rb");

Any idea what I'm doing wrong?
Re: Problem with fopen
November 23, 2008 03:31PM
Both should produce the same results. What problems are you having with the second one?
Re: Problem with fopen
November 23, 2008 04:13PM
When I use the second one inputFile stays NULL. That means that the file isn't found. Should I use a different type than char?
Re: Problem with fopen
November 23, 2008 04:23PM
I think that variable is too large for char. Try using string instead of char.
Re: Problem with fopen
November 23, 2008 05:34PM
*char is the correct type for fopen(). The only way I can see the second one returning NULL, is if you've somehow corrupted your path variable. Right before fopen(), insert a printf("path=%s\n", path) and double check that path is still pointing to your file.

If you're still stuck, post the source code and I'll try it out on my end.
Re: Problem with fopen
November 23, 2008 06:02PM
I printed path right before fopen and it ok. I'm reading the path from a text file. Can that be the problem?
Re: Problem with fopen
November 23, 2008 07:43PM
You may have some unprintable control character in the path that you read in, to be 100% sure you're passing the same info to fopen(), add this right before you call fopen():

printf("path strings are %s.\n", strcmp(path, "/config.txt") ? "not equal" : "equal");

If the above says the paths are equal, then I'm out of ideas.
Re: Problem with fopen
November 23, 2008 08:05PM
They are not equal! Maybe there are blank spaces at the beginning or end. How can I delete those? Or what else could be the cause that they are not equal?

Thanks for your answers.
Re: Problem with fopen
November 23, 2008 08:53PM
Next, I would print out the contents pointed to by your path variable, and see where things start to differ from what you expected. Something like this will dump out the path array (this prints both the character and the hex value for that character):

for(i=0; i<=strlen(path); i++) {
   //note: this prints a dash, -, for non-printables
    printf("path[%d] = %c (0x%x)\n", i, isprint(path) ? path : '-', path);   
}
Re: Problem with fopen
November 24, 2008 05:20PM
Thanks Michael. It seems that there are spaces and/or "\n" at the end of the read path. Is there any way to remove these?
Re: Problem with fopen
November 24, 2008 07:34PM
Great, glad to hear you found the issue. As for trimming white space, do a search on ''c char trim trailing spaces", and you should find some code examples. Here's one that looks like it would work.
Re: Problem with fopen
November 24, 2008 10:07PM
How do I call the function "char *trimwhitespace(char *str)"?

I get an error when I try it like this:
char * newPath = trimwhitespace(path);



Edited 1 time(s). Last edit at 11/24/2008 10:07PM by SpaceJump.
Re: Problem with fopen
November 24, 2008 11:17PM
I would call it like:

path = trimwhitespace(path);



Edited 2 time(s). Last edit at 11/24/2008 11:24PM by Michael.
Re: Problem with fopen
November 25, 2008 08:05AM
When I do it like that I get an error:
error: previous implicit declaration of 'trimwhitespace' was here

This is how my source file is setup:
int main(int argc, char **argv) {
...
path = trimwhitespace(path);
...
}
char *trimwhitespace(char *str) {
  char *end;

  // Trim leading space
  while(isspace(*str)) str++;

  // Trim trailing space
  end = str + strlen(str) - 1;
  while(end > str && isspace(*end)) end--;

  // Write new null terminator
  *(end+1) = 0;

  return str;
}
Re: Problem with fopen
November 25, 2008 08:26AM
yeck...
First off, you'd need to use prototypes to access trimwhitespice.
Second, USE MALLOC ON THOSE SUCKERS!
Pointers do not allocate themselves! You're lucky you haven't had a segfault yet!
Re: Problem with fopen
November 25, 2008 08:50AM
What are prototypes? Sorry, I only have experience in Java Development so my approach in C is similar. Can you explain further?
Re: Problem with fopen
November 25, 2008 08:52AM
A prototype is where you put the declaration of the function up at the top of the file. Normally people just create a header file, where they store all of their prototypes, so that if any functions in another file want to access the function, they just include that header.
And for the malloc thing, just google malloc and I'm sure you'll find something helpful.
Re: Problem with fopen
November 25, 2008 08:55AM
Quote
SpaceJump
When I do it like that I get an error:
error: previous implicit declaration of 'trimwhitespace' was here

This is how my source file is setup:
int main(int argc, char **argv) {
...
try
char *trimwhitespace(char *str);

int main(int argc, char **argv) {
...
   path = trimwhitespace(path);
...
}

char *trimwhitespace(char *str) {
  char *end;

  if (str == NULL) return str;

  // Trim leading space
  while((*str != '\0') && (isspace(*str))) str++;

  // Trim trailing space
  end = str + strlen(str) - 1;
  while(end > str && isspace(*end)) end--;

  // Write new null terminator
  *(end+1) = '\0';

  return str;
}
Re: Problem with fopen
November 25, 2008 08:58AM
Thanks bushing, that works :)

Edit: Just wanted to say that it's great how helpful all you are here.



Edited 1 time(s). Last edit at 11/25/2008 08:58AM by SpaceJump.
Sorry, only registered users may post in this forum.

Click here to login