8 Replies - 533 Views - Last Post: 15 February 2012 - 04:46 PM Rate Topic: -----

#1 MitchKrendel  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 09-February 12

How to extend my program so that outputted file is reversed?

Posted 09 February 2012 - 08:22 PM

Hello everyone. I am extremely new to C programming and I would greatly appreciate some input. I apologize if my questions come off us unclear or rude. Ok, basically I'm working on a program that reads and displays text from a text file when I type into a command line (got that part working so far). Now I'm trying to add onto what I've got so far with code that would allow that same text output reversed (ie: Hello there! = !ereht olleH) through the command line. I understand the concept behind reversing strings in Java, my problem here being I do not know how to properly add such lines of code (syntax wise) to what I've come up so far in C. I've been frustratingly scouring the web and trying many things but I seem to have reached an impass. Any help would be greatly appreciated. Thanks!

oh yes, and so far the command line I've been using to output the text is basically <filename.c> <filename.txt>. Ideally I am trying to achieve the reverse by typing 3 arguments.

#include <stlib.h>
#include <stio.h>

int main( int argc, char *argv[] )
{
   if (argc != 2)
     {
         printf("%d", argc);
         printf("usage: %s text.txt", argv[0]);
      }
      else
      {
           FILE *fp = fopen( argv[1], "r" );
            if ( file == 0 )
            {
               printf( "Could not open file\n" );
            }
             else
            {
                int x;
                while ( ( x = fgetc ( fp ) ) !EOF )
                  {
                      printf( "%c", x );
                  }
             fclose(fp);
             }
      }
    }



Is This A Good Question/Topic? 0
  • +

Replies To: How to extend my program so that outputted file is reversed?

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: How to extend my program so that outputted file is reversed?

Posted 09 February 2012 - 08:26 PM

How would you do it in Java?
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: How to extend my program so that outputted file is reversed?

Posted 09 February 2012 - 08:55 PM

That wasn't just an idle question. It was an invitation to you to demonstrate that you're actively trying to complete your assignment and not simply hoping that someone will write it for you (which is against the policies of this board). So far you haven't demonstrated any attempt at the actual problem.

The syntax of C is very similar to the syntax of Java, so if you know how to write it in Java you should be able to do it, or get very close to doing it, in C.
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: How to extend my program so that outputted file is reversed?

Posted 13 February 2012 - 09:44 AM

You requested another chance for your question, so here it is. You stated that you want your C program to reverse the strings that it reads from a file, and that you know how to do this in Java (not really important, but that's what you said), and that nothing that you have tried to accomplish this in C has worked.

However, the code that you posted merely reads strings from a file and echoes exactly the same strings to the display (or it would do that after you correct various minor syntax errors). But as it stands right now, we see no effort whatsoever towards reversing a string, and the code that you have written still isn't compilable due to syntax errors.

So the ball is in your court. First, you should fix this code so it compiles and runs correctly -- the compiler error messages tell you where the errors are -- read them and deal with them one by one until it works. If there is a particular error message that you don't understand, ask about it specifically (and post your revised code at that point, along with the exact compiler error message).

After that's done, you should start working on code to reverse the strings and then you can ask specific questions about specific aspects of that process that give you trouble -- not a general question like "how can I reverse a string?"
Was This Post Helpful? 2
  • +
  • -

#5 MitchKrendel  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 09-February 12

Re: How to extend my program so that outputted file is reversed?

Posted 15 February 2012 - 07:43 AM

Apologies for any ignorance on my part in regards to proper posting etiquette, as this is the first time I've ever posted to a programming forum.
Basically what I am attempting to achieve is outputting the text from a file reversed by inputting : filename.c -reverse textfile.txt .
I included the updated code I probably should have submitted originally which includes my attempt to create a reverse flag (correct term?). Thank you for any available assistance.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main( int argc, char *argv[] )
   {
        char line[200];

        /* user specified -reverse flag */
        /* filename should be argv[2] */
        {

     if (argc != 2)
        {
                printf("%d" , argc);
                printf("usage: %s abcd.txt", argv[0]);
        }
        else
        {
               FILE *file = fopen( argv[1], "r" );
                if ( file == 0 )
                {
                        printf( "Could not open file\n" );
                }
                else
                {       int x;
                        while ( ( x = fgetc (file ) ) !=EOF )
                        {
                                printf( "%c", x );

                        }
  if ( strcmp (argv[1], "-reverse") == 0 )
  {
    fprintf( stderr, "ERROR: Invalid usage\n" );
    fprintf( stderr, "USAGE: %s abcd.txt", argv[1] );
    return EXIT_FAILURE;
  }
else
{
  FILE * file = fopen( argv[1], "r" );

  if ( file == 0 )
  {
    fprintf( stderr, "ERROR: could not open file %s\n", argv[1] );
    perror( "could not open filesss" );
    return EXIT_FAILURE;
  }
  else
  {



  while ( fgets( line, 200 , file ) != NULL )
  {
        int y;
        int len = strlen ( line );
    for ( y = len - 1 ; y >= 0 ; y-- )
    {
      printf( "%c", line[y] );

    }

  }


          fclose( file );

  return EXIT_SUCCESS;
}
}
}
}
}
}


Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,514
  • Joined: 23-August 08

Re: How to extend my program so that outputted file is reversed?

Posted 15 February 2012 - 07:59 AM

Let's try to bring a little sanity to the indentation shall we?

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main( int argc, char *argv[] )
{
   char line[200];

   /* user specified -reverse flag */
   /* filename should be argv[2] */
   {
      if (argc != 2)
      {
         printf("%d" , argc);
         printf("usage: %s abcd.txt", argv[0]);
      }
      else
      {
         FILE *file = fopen( argv[1], "r" );
         if ( file == 0 )
         {
            printf( "Could not open file\n" );
         }
         else
         {
            int x;
            while ( ( x = fgetc (file ) ) !=EOF )
            {
               printf( "%c", x );
            }
            if ( strcmp (argv[1], "-reverse") == 0 )            {
               fprintf( stderr, "ERROR: Invalid usage\n" );
               fprintf( stderr, "USAGE: %s abcd.txt", argv[1] );
               return EXIT_FAILURE;
            }
            else
            {
               FILE * file = fopen( argv[1], "r" );
               if ( file == 0 )               {
                  fprintf( stderr, "ERROR: could not open file %s\n", argv[1] );
                  perror( "could not open filesss" );
                  return EXIT_FAILURE;
               }
               else
               {
                  while ( fgets( line, 200 , file ) != NULL )
                  {
                     int y;
                     int len = strlen ( line );
                     for ( y = len - 1 ; y >= 0 ; y-- )
                     {
                        printf( "%c", line[y] );
                     }
                  }
                  fclose( file );
                                                
                  return EXIT_SUCCESS;
               }
            }
         }
      }
   }
}



strcmp returns 0 if the strings being compared are the same; you are treating it as if that were not the case. You need to fix that. You also open your file at line 19 and, without closing it, open it again on line 38.

You need to return a value from main in all your code paths.
Was This Post Helpful? 3
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: How to extend my program so that outputted file is reversed?

Posted 15 February 2012 - 08:06 AM

If you are trying to add information to your command line you will need change your logic that is determining if you entered the proper number of arguments. In C argv[0] is your programs file name, argv[1] would be the first parameter you entered on the command line. So when you enter: "yourProgramName filename.c -reverse textfile.txt" argc will report 4 arguments passed. The first argument (argv[0]) is "yourProgramName", the name of the program you are running. The second argument (argv[1] is "filename.c". The third argument (argv[2]) is "-reverse" and the fourth (argv[3]) is "textfile.txt".

Jim
Was This Post Helpful? 1
  • +
  • -

#8 MitchKrendel  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 09-February 12

Re: How to extend my program so that outputted file is reversed?

Posted 15 February 2012 - 04:24 PM

Thanks to everyone so far for all your patience and help. I wont keep this going much longer...but what about what I have now? So far I have the first part working ie : <filename> abcd.txt is outputting what I wanted. I did the second part (to output the same thing reversed by : <filename> -reverse abcd.txt) by starting a seperate section of if statements. Doesn't work :sad3: , says "could not open file". Thanks!

 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main( int argc, char *argv[] )
{
        char line[200];



        if (argc != 2)  
        {
                printf("%d", argc);    
                printf("usage:%s abcd.txt", argv[0]);

        }
        else
        {
                FILE *file = fopen( argv[1], "r" );
                if (file == 0)
                {
                        printf("Could not open file\n");
                }
                else
                {
                        int x;

                      while (( x = fgetc(file)) !=EOF )
                        {
                                printf("%c", x);
                        }

                fclose(file);

        }


       if  (argc != 3)
        {
            printf("%d", argc);

            printf("usage:%s abcd.txt", argv[1]);
        }
        else
        {
            FILE *file = fopen(argv[2], "r" );


           if (strcmp (argv[2], "-reverse") == 0)
                {
                        printf("Error\n");
                }
                else
                {
                 while (fgets(line, 200, file)!= NULL)

                        {
                                int y;
                                int len = strlen (line);
                          for ( y = len -1; y >=0; y--)
                                {
                                   printf("%c",line[y]);
                                }
                        }

                fclose( file);

                }


}

}

        return EXIT_SUCCESS;
}


Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: How to extend my program so that outputted file is reversed?

Posted 15 February 2012 - 04:46 PM

It isn't necessary for every "if" to have an "else", and in this case some of those nested if/else constructs are making the program unnecessarily confusing and hard to read.

I would set it up as follows (in pseudocode):

if argc NEQ 2
...if argc EQ 3 AND argv[2] EQ "-reverse"
......leave this case empty, i.e., do nothing
...else print usage message and exit

COMMENT: notice, the above "else" is for the second "if"; there is no "else" for the first "if"

COMMENT: if you get to this point you know that either there are 2 arguments, or there are 3 arguments and the third one is "-reverse"

open file
if filepointer EQ 0
...print file error message and exit

COMMENT: no else for the preceding if

read the file into the char array

if argc EQ 2
...print it forward
else
...print it backward

close file

----------------

I believe that will do what you want in a more straightforward fashion.

edit: Sorry, I had a logical error in the second if statement. I think it's OK now.

This post has been edited by r.stiltskin: 15 February 2012 - 04:58 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1