Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Please please help with this simple Q from a C++ newbie

Please please help with this simple Q from a C++ newbie

Scheduled Pinned Locked Moved C / C++ / MFC
c++perllinuxhelptutorial
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    meixiang6
    wrote on last edited by
    #1

    Please please help with this simple Q from a C++ newbie....I spent hours trying to find the answer online... I want to run this on a linux box. Open a file containing a list of file names and then run a linux commands on each of the list for example: The file contains a list of files names eg: test test1 test2 test3 How do I code in C so that it opens the file and then foreach of the elements do a system command eg: ls in perl it would be something like: #!/usr/local/bin/perl while(<>){ chomp; $cmd="ls $_"; system($cmd); print "$cmd\n"; } Thanks in advance!

    S 1 Reply Last reply
    0
    • M meixiang6

      Please please help with this simple Q from a C++ newbie....I spent hours trying to find the answer online... I want to run this on a linux box. Open a file containing a list of file names and then run a linux commands on each of the list for example: The file contains a list of files names eg: test test1 test2 test3 How do I code in C so that it opens the file and then foreach of the elements do a system command eg: ls in perl it would be something like: #!/usr/local/bin/perl while(<>){ chomp; $cmd="ls $_"; system($cmd); print "$cmd\n"; } Thanks in advance!

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      Use fgets to read individual lines from a file. Use strcat to append the file name to the command (just make sure the command is in a long enough string to start with). C has a system function just like Perl as well. It's easier in C++, using std::getline to read lines from a file and the std::string class to build the command:

      #include <stdlib.h>
      #include <iostream>
      #include <fstream>
      #include <string>

      int main(int argc, char** argv)
      {
      std::ifstream f(argv[1]);

      std::string s;
      while (std::getline(f, s))
      {
      system((std::string("ls -l ") + s).c_str());
      }
      }

      Easy enough (and works on OS X, which is close enough to Linux)

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      M 1 Reply Last reply
      0
      • S Stuart Dootson

        Use fgets to read individual lines from a file. Use strcat to append the file name to the command (just make sure the command is in a long enough string to start with). C has a system function just like Perl as well. It's easier in C++, using std::getline to read lines from a file and the std::string class to build the command:

        #include <stdlib.h>
        #include <iostream>
        #include <fstream>
        #include <string>

        int main(int argc, char** argv)
        {
        std::ifstream f(argv[1]);

        std::string s;
        while (std::getline(f, s))
        {
        system((std::string("ls -l ") + s).c_str());
        }
        }

        Easy enough (and works on OS X, which is close enough to Linux)

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        M Offline
        M Offline
        meixiang6
        wrote on last edited by
        #3

        Thanks very much Stuart! Any chance of typing the entire code in C as well? thanks a lot again!

        S 1 Reply Last reply
        0
        • M meixiang6

          Thanks very much Stuart! Any chance of typing the entire code in C as well? thanks a lot again!

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          meixiang6 wrote:

          Any chance of typing the entire code in C as well?

          I'll leave that as an exercise for the reader...you remember I said it was easier in C++ :-) It's not too much different - fgets is roughly equivalent to getline. Rather than adding string objects together, you use strcpy to copy the command into a character buffer, then strcat to add the line you've read from the file to the end of the buffer. Then you pass the buffer to the system command.

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          M 1 Reply Last reply
          0
          • S Stuart Dootson

            meixiang6 wrote:

            Any chance of typing the entire code in C as well?

            I'll leave that as an exercise for the reader...you remember I said it was easier in C++ :-) It's not too much different - fgets is roughly equivalent to getline. Rather than adding string objects together, you use strcpy to copy the command into a character buffer, then strcat to add the line you've read from the file to the end of the buffer. Then you pass the buffer to the system command.

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            M Offline
            M Offline
            meixiang6
            wrote on last edited by
            #5

            thanks Stuart for the information, I am a very new newbie and have no idea how that looks like in C, any chance of someone writing that in C ? apologizes for the inconvenience

            S 1 Reply Last reply
            0
            • M meixiang6

              thanks Stuart for the information, I am a very new newbie and have no idea how that looks like in C, any chance of someone writing that in C ? apologizes for the inconvenience

              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #6

              You'll gain a lot more learning how to do it yourself. This page[^] has code that reads a file line by line (search for 'fgets').

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              M 1 Reply Last reply
              0
              • S Stuart Dootson

                You'll gain a lot more learning how to do it yourself. This page[^] has code that reads a file line by line (search for 'fgets').

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                M Offline
                M Offline
                meixiang6
                wrote on last edited by
                #7

                Thanks Stuart, I got it working with this, not sure if the code is right thou but it works... #include #include int main() { FILE *f; char string[1000]; char str[1000]; f = fopen("file.txt", "r"); if(!f) { printf("Couldn't open file.txtn"); return 1; } while(fgets(str, 1000, f)) { /* printf("%s", str); */ strcpy (string, "ls -la "); strcat (string, str); system (string); } fclose(f); return 0; }

                modified on Thursday, February 19, 2009 8:56 PM

                D S 2 Replies Last reply
                0
                • M meixiang6

                  Thanks Stuart, I got it working with this, not sure if the code is right thou but it works... #include #include int main() { FILE *f; char string[1000]; char str[1000]; f = fopen("file.txt", "r"); if(!f) { printf("Couldn't open file.txtn"); return 1; } while(fgets(str, 1000, f)) { /* printf("%s", str); */ strcpy (string, "ls -la "); strcat (string, str); system (string); } fclose(f); return 0; }

                  modified on Thursday, February 19, 2009 8:56 PM

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  meixiang6 wrote:

                  char string[1000];

                  I know this is being nit-picky, but you might want to make this 7 bytes larger than str. If str contains 999 bytes, copying that plus "ls -la " is going to overflow string.

                  "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  1 Reply Last reply
                  0
                  • M meixiang6

                    Thanks Stuart, I got it working with this, not sure if the code is right thou but it works... #include #include int main() { FILE *f; char string[1000]; char str[1000]; f = fopen("file.txt", "r"); if(!f) { printf("Couldn't open file.txtn"); return 1; } while(fgets(str, 1000, f)) { /* printf("%s", str); */ strcpy (string, "ls -la "); strcat (string, str); system (string); } fclose(f); return 0; }

                    modified on Thursday, February 19, 2009 8:56 PM

                    S Offline
                    S Offline
                    Stuart Dootson
                    wrote on last edited by
                    #9

                    Aside from what David said, that code's fine :-)

                    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups