05-02-2012 09:25 PM - edited 05-03-2012 08:04 AM
I'm trying to write and then read a bit of data, and it's not working. The file size is coming back as "0" after I write the file:
char* ptr = NULL;
write_file("./data/test.txt", "testing!");
read_file("./data/test.txt", &ptr);
fprintf(stderr, "HERE: %s\n", ptr);
void write_file(char* file_name, char* data)
{
FILE* file = fopen(file_name, "w");
if (file == NULL)
{
fprintf(stderr, "Cannot write to file: %s\n", file_name);
}
else
{
fprintf(file, data);
}
close(file);
}
int read_file(char* file_name, char** data)
{
FILE* file = fopen(file_name, "r");
if (file == NULL)
{
fprintf(stderr, "Cannot read from file: %s\n", file_name);
}
else
{
struct stat st;
int rc = stat(file_name, &st);
if (rc)
{
return -1;
}
long num_bytes = st.st_size;
*data = malloc(num_bytes + 1);
fread(*data, 1, num_bytes, file);
(*data)[num_bytes] = NULL;
}
close(file);
return 0;
}
Not sure what I'm doing wrong here...
Solved! Go to Solution.
05-03-2012 01:31 AM - edited 05-03-2012 07:13 AM
can you edit your post and place your code inside a code field? (the symbol with the small c)
have you set the permission in the bar-descriptor.xml?
05-03-2012 04:00 AM - edited 05-03-2012 04:02 AM
No real Idea, but some hints:
05-03-2012 08:05 AM
Not sure what the "small c" thing is, but I flipped to the HTML tab and put it in <pre>...</pre>, and that seems to have accomplished the same thing.
I haven't set that permission in bar-descriptor.xml, since I read that is only necessary if you try and write to the shared folders.
05-03-2012 08:07 AM
By "directory data", you mean create the directory? I didn't, since "data" is listed as one of the standard folders for each app, but that is worth checking in terms of whether it's actually there. The "fopen" call does seem to succeed, so I would think that indicates the directory is there.
I have stepped through the code so I know it isn't returning error codes, at least upon opening the file.
Oh, interesting... fclose... I had copied and pasted some other sample code, maybe they were wrong to use "close". I'll check that.
05-03-2012 08:13 AM - edited 05-03-2012 08:36 AM
I applied your "fclose" suggestion and that appears to have worked:
int write_file(char* file_name, char* data)
{
int rc = 0;
FILE* file = fopen(file_name, "w");
if (file == NULL)
{
fprintf(stderr, "Cannot open file for writing: %s\n", file_name);
rc = -1;
}
else
{
rc = fprintf(file, data);
if (rc)
{
fprintf(stderr, "Cannot write to file: %s\n", file_name);
}
fclose(file);
}
return rc;
}
int read_file(char* file_name, char** data)
{
int rc = 0;
FILE* file = fopen(file_name, "r");
if (file == NULL)
{
fprintf(stderr, "Cannot open file for reading: %s\n", file_name);
rc = -1;
}
else
{
struct stat st;
int rc = stat(file_name, &st);
if (rc)
{
fprintf(stderr, "Cannot read from file. File size couldn't be determined: %s\n", file_name);
}
else
{
long num_bytes = st.st_size;
*data = malloc(num_bytes + 1);
rc = fread(*data, 1, num_bytes, file);
if (rc)
{
fprintf(stderr, "Cannot read from file: %s\n", file_name);
}
else
{
(*data)[num_bytes] = NULL;
}
fclose(file);
}
}
return rc;
}
I should go back and find that stack overflow code and note this issue. The IDE actually underlined that call to close with a warning... one of those times I should have looked closer. Thanks!
05-10-2012 09:52 AM
05-10-2012 09:57 AM
Yes, it was simply "fclose" that did the trick. I didn't employ any of the other suggestions.