Originally Posted by
tabstop
Then you didn't try hard enough. You need to work one line at a time, so you need to read one line at a time, and fgets is the main line-based input function. You do need to make sure you have a char array handy to put the input into.
ahh i see its like something on what i was working with at the begining, i had something very close to what u were talking about when i started working with the program. Can you check out the one i worked with at first, i know its practically finished, if i could get that to run properly i can have the one above working in no time. i get these errors with the codes below:
C:\Documents and Settings\Administrator\My Documents\100_php_scripts\Untitled1.c In function `flipstring':
68 C:\Documents and Settings\Administrator\My Documents\100_php_scripts\Untitled1.c [Warning] function returns address of local variable
These are the codes: Sorry i know indentation is crappy on this one.
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// Our function declaration saying it takes one string pointer and returns one string pointer.
char * flipstring(char *stringtoflip);
int main() {
// Strings to hold our lines (forward and backward)
char string[256];
char flipped[256];
// Our file pointers (one in and one out)
FILE * infile;
FILE * outfile;
// Open files to valid txt files
infile = fopen("input.txt","r");
outfile = fopen("output.txt","w");
// Loop through the input file reading each line up to 256 chars
while (fgets(string,256,infile) != NULL) {
printf("The value read in is: %s\n", string);
// Flip the string and copy it to our "flipped" string
strcpy(flipped,flipstring(string));
// Write "flipped" to output
printf("The value written is: %s\n",flipped);
fputs(flipped,outfile);}
// Close files
fclose(infile);
fclose(outfile);
return 0;
}
// Flips strings by taking incoming string, loop in reverse
// and write each char to reverse string. Return reverse string.
char * flipstring(char *stringtoflip) {
char reverse[256];
int j = 0;
int i= strlen(stringtoflip)-1;
// Loop through each char of our string in reverse
for( i ;i>=0; i--)
{
// If it is a newline, just ignore for now.
if (stringtoflip[i] != '\n') {
reverse[j]=stringtoflip[i];
j++;
}
}
// Terminate the new reverse string.
reverse[j] = '\0';
// Return reverse string back to main()
return reverse;
}