A couple of comments/nitpicks, which may or may not solve your problem 🙂
if (!str || !(*str))
return NULL;
Don’t do that. Don’t return NULL on empty strings, fputs() will barf. In my experience, it’s better to a) assert that the str pointer is non-null, b) return the empty string.
begin=str+strlen(str)+1; *begin='\0'; //??
There should be no need to terminate the string, since it’s already terminated.
void main(void)
Nah, main() returns an int.
while (!feof(fsrc))
This won’t work. You need to do some IO before you can test for feof()/ferror(). IMHO it’s better to simply loop on fgets().
while (fgets(line, sizeof line, fsrc) {
...
}
It may be a good idea to drop the input and output files, and simply read from stdin and write to stdout, at least while testing. The functionality you’re trying to implement is already available in a UNIX shell (man rev). Using stdin/stdout makes it easier to test and compare results with the results from rev.
Also, keep in mind that fgets() won’t remove the \n from the string. input like “foo\n” becomes “\noof”, which is probably not what you want.
Here’s a snippet which illustrates my comments in code. It doesn’t solve all problems, but should be sufficient to get you going.
#include <stdio.h>
#include <string.h>
#include <assert.h>
void reverse(char *str)
{
char *begin, *end, c;
size_t n;
assert(str != NULL);
n = strlen(str);
if (n == 0)
return;
for (begin = str, end = str + n - 1; begin < end; begin++, end--) {
c = *begin;
*begin = *end;
*end = c;
}
}
int main(void)
{
char line[1000];
while (fgets(line, sizeof line, stdin)) {
reverse(line);
fputs(line, stdout);
}
}
HTH
solved Problems with reversing line into file [closed]