Something Wrong with my function in Linux!

This is a discussion on Something Wrong with my function in Linux! within the C Programming forums, part of the General Programming Boards category; Guys i got this program working Fine on windows its output is just like how i want it, perfect! But ...

  1. #1
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235

    Unhappy Something Wrong with my function in Linux!

    Guys i got this program working Fine on windows its output is just like how i want it, perfect! But when i go to a linux machine the output is not how it should be, whats wrong with it, i believe its something with the logic of my function, but i can't find it. Could anyone help me rewrite it, or give ideas please.

    For example:
    The original file has:



    Jack Spratt
    could eat no fat.
    His wife
    could eat no lean.


    The output file will should look like:


    ttarpS kcaJ
    .taf on tae dluoc
    efiw siH
    .nael on tae dluoc



    But IN LINUX I GET:

    ttarpS kcaJ

    .taf on tae dluoc

    efiw siH
    .nael on tae dluoc


    I need to get rid of those blank lines, and format the output as is on windows.
    These are the codes i am working with:

    Code:
    #include <stdio.h> 
    #include <string.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);  
      fputs("\n",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) {  
      
      static 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;  
     }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Have you checked the input file (input.txt) to make sure there's not some extra newlines in the file?

  3. #3
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by swoopy View Post
    Have you checked the input file (input.txt) to make sure there's not some extra newlines in the file?
    Yes the input file is alright, iv checked that, on windows it runs perfectly well, but on linux i get that result.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I ran your code and it works for me (no blank lines). If I add a newline at the very end of the file, I do get an extra line at the end of the output file.

    You might try deleting output.txt, and then rerunning.

  5. #5
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235

    Smile

    Quote Originally Posted by swoopy View Post
    I ran your code and it works for me (no blank lines). If I add a newline at the very end of the file, I do get an extra line at the end of the output file.

    You might try deleting output.txt, and then rerunning.

    Hey thanks Swoopy thanks a lot, after all it was something with the input text file. It was because i was taking the text file from windows to Ubuntu, and copying it to its text editor. What i did was to retype everything back manually, and it worked. Thanks a lot for your time,and help.

    thank u!

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >It was because i was taking the text file from windows to Ubuntu, and copying it to its text editor.
    I don't know what program you used to transfer the file to Ubuntu, but there may be a way to do the transfer in text mode (versus binary mode). Windows uses both <CR> and <LF> to separate a line, whereas Linux uses a single character to separate lines, which explains why you were getting extra blank lines. Anyway glad you got it working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  3. On the wrong track for a Function Prototype
    By Mohammed in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2003, 01:16 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21