Thread: string reversal

  1. #1
    Unregistered
    Guest

    string reversal

    Can somebody tell me why this only reverses the last word in the data file, and not all the texti in the file?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	FILE *datain, *dataout;
    
    	char filenamein[15], filenameout[15], str[] = "";
    
    	printf("Enter input filename: ");
    	gets(filenamein);
    	printf("Enter output filename: ");
    	gets(filenameout);
    
    	datain = fopen(filenamein, "r");
    	dataout = fopen(filenameout, "w");
    		
    	while (! feof(datain))
    	
    	fscanf(datain, "%s", str);
    	fprintf(dataout,strrev(str));
    return 0;
    }

  2. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    I could, but then I'd have to kill you

  3. #3
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    Well, either that or I could ask you to review loops





    ooooh...that and look up the fucntion fclose()

  4. #4
    Unregistered
    Guest
    ok. I got that part, but what about spaces

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	FILE *datain, *dataout;
    
    	char filenamein[15], filenameout[15], str[] = "";
    
    	printf("Enter input filename: ");
    	gets(filenamein);
    	printf("Enter output filename: ");
    	gets(filenameout);
    
    	datain = fopen(filenamein, "r");
    	dataout = fopen(filenameout, "w");
    		
    	while (! feof(datain))
    	{	
    		fscanf(datain, "%s", str);
    		fprintf(dataout,strrev(str));
    	}
    	fclose(datain);
    	fclose(dataout);
    		
    	
    return 0;
    }
    i have this in my input:

    this is a test
    this is the output i get. It is all in one big sting with no spaces.

    sihtsiatset

  5. #5
    Unregistered
    Guest
    ok i got it.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
    	FILE *datain, *dataout;
    
    	int x = 100000000;
    	char filenamein[15], filenameout[15], str[] = "";
    
    	printf("Enter input filename: ");
    	gets(filenamein);
    	printf("Enter output filename: ");
    	gets(filenameout);
    
    	datain = fopen(filenamein, "r");
    	dataout = fopen(filenameout, "w");
    		
    	while (! feof(datain))
    	{		
    		fgets(str, x, datain);
    		fprintf(dataout,strrev(str));
    	}
    	fclose(datain);
    	fclose(dataout);
    		
    	
    return 0;
    }
    Can somebody help me with the code for x though, so x can be infinite. I tried using x++ within the while loop, but it screwed up the letters.

  6. #6
    Unregistered
    Guest
    the x in fgets only refers to the number of characters maximum to read on that line, and since in general integers are maximum of about 65535 (unsigned) or something like that so if your lines in the file are 100000000 characters long then i recommend declaring your x variable like:
    long x = 100000000;

    or decrease the size of x to somethin like:
    int x = 32000;

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    What if this fails?

    datain = fopen(filenamein, "r");
    dataout = fopen(filenameout, "w");

    You should check if the pointers returned by fopen are not NULL.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >gets(filenamein);
    Never use gets, it's unsafe to the extreme. Use fgets instead.
    fgets ( filenamein, sizeof filenamein, stdin );

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM