1

So I have to build a program in C that practically takes a command from keyboard , split it into tokens that are stored in an array and use those tokens as input to "execv" (a command in ubuntu) , I chose the command "uname" with the parameter "-a", but I got stuck at the array storing, as it stores only the first letter of the token which was split.

#include <stdio.h>
#include<stdlib.h>
#include <string.h>   /*strtok strcpy*/
#include<malloc.h>   /*malloc*/
#include <sys/types.h> /* pid_t */
#include <sys/wait.h>  /* waitpid */
#include <unistd.h>    /* _exit, fork */

int main()
{
    int i=0;
    char *cuvinte[256]; // words
    char comanda[256];  //command

    printf("Introduceti comanda: "); //enter the command
    fgets(comanda,sizeof(comanda),stdin); // read the command
    char *c = strtok(comanda," "); // split it into tokens

    while(c!=0)
    {
        cuvinte[i] = malloc( strlen( c ) + 1 ); // alocate memory for the array
        strcpy(cuvinte[i++],c); //copying tokens into array
        printf("%s\n",c); // printing them
        c=strtok(NULL, " ,.!?");
        }
    printf("Sunt %d elemente stocate in array! \n\n",i); //no of tokens stored
    printf("Primul cuvant este: \n\n\n"+*cuvinte[1]); //should print the first word


    /*i got stucked here because of the tokens */ 

/*face un proces copil*/
      pid_t pid=fork();
        if (pid==0) { /* procesul copil*/
        static char *argv[]={"/bin/uname","-a",NULL};
        execv(argv[0],argv);
        exit(127); /*in caz ca execv da fail*/
        }
        else { /* pid!=0; proces parinte */
        waitpid(pid,0,0); /* asteapta dupa copil */
        }
    //getch();
    return 0;
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Alexandru Ianas
  • 95
  • 1
  • 11
  • In C, you can't concatenate strings with `+`. Your printing command should be `printf("Primul cuvant este: %s\n", cuvinte[1]);` – M Oehm Nov 05 '14 at 11:28

2 Answers2

0

I think your problem is the printf("Primul cuvant este: \n\n\n"+*cuvinte[1]); statement. This dereferences the first character of the second item in cuvinte. What I think you want to do instead is printf("Primul cuvant este: %s \n\n\n",cuvinte[0]);.

Live version.

struct
  • 604
  • 7
  • 13
  • @AlexandruIanas: Then work on it and once you've been stuck for a while post another question. – Lightness Races in Orbit Nov 05 '14 at 11:57
  • @LightnessRacesinOrbit ive edited this question so i wont put another and spam because i'm 100% sure its a small mistake again,if you look up ive added the code I made below the first one ! – Alexandru Ianas Nov 05 '14 at 12:11
  • 1
    @AlexandruIanas: No, please don't do that. This isn't a forum - one question per question and don't fundamentally change it afterwards! I reverted your edit. Make a new question as I said or, if you think it's just a small mistake, you're better off finding a chat room to ask in. – Lightness Races in Orbit Nov 05 '14 at 12:13
  • 1
    @LightnessRacesinOrbit Ok, sorry , il take your advise , il work on it and if I can't make it work I will post a new question also I apolagize for my rusty english and if i did something wrong. Thanks! – Alexandru Ianas Nov 05 '14 at 12:20
-1

You're assigning the ouput of strtok (via pointer) to a single character variable.

Zeiss Ikon
  • 481
  • 4
  • 13
  • _"Now working on re-learning C to have a grounding for C++ to write my own tools/toys in Kubuntu on a Core2Quad."_ Yikes! If you want to learn C++ then learn C++; don't ruin it by learning C first. C does not give you a grounding in C++: it teaches you _entirely different and inappropriate_ idioms. – Lightness Races in Orbit Nov 05 '14 at 11:58
  • I learned ANSI C in 1990; I have reference materials and I remember a lot of it, but I have no place to start on C++ -- if it's that different from ANSI C, it'd be like trying to teach myself assembler, knowing only a smattering of BASIC. I'm ahead to get comfortable programming again before I jump into GUI and objects and inheritance and such (IMO). – Zeiss Ikon Nov 05 '14 at 21:50
  • Honestly, better to learn the basics from a different language (not C) if your ultimate goal is to end up using C++. C will mislead you! – Lightness Races in Orbit Nov 06 '14 at 01:18
  • I'm not trying to "learn the basics" of C++ by relearning C, I'm trying to recover the mindset and general "stepwise" thought process of programming. Tempting to suggest, however, if C++ is such a departure from C (pretty much the epitome of classical programming), someone (back around 1990) should probably have named it differently... – Zeiss Ikon Nov 06 '14 at 02:17
  • Yes, I understood what you were saying. And, yes, if Mascitti had known how far C++'s idioms and semantics would diverge from C, eight years before the _first_ standard, I'm sure he'd have called it something else. (Actually, the name was first used in 1983, so _fifteen_ years prior.) It _does_ make sense to a degree, since C++ evolved out of C. However, that is not the same as saying that learning C gives any sort of useful grounding in C++. It does not -- quite the contrary. To regain your programming mojo before learning C++ I suggest Python or something. – Lightness Races in Orbit Nov 06 '14 at 10:04
  • Right, but I'd be learning Python completely from scratch, just as I'd be starting from scratch to learn C++. I tried learning object-oriented on my own once before (using a very obscure product called "Interpreting Zoomer Langugage", IZL -- for the GEOS OS on the desktop and Casio Zoomer), with zero success; I couldn't even get the buttons to show inheritance of the frame's properties (or make sense of doing so). – Zeiss Ikon Nov 06 '14 at 11:21
  • Yeah but you're not listening to me. – Lightness Races in Orbit Nov 06 '14 at 11:26
  • I'm hearing you say "abandon C completely if you want to learn C++". I'm hearing you say "learn Python from scratch rather than recover the C you once knew, because C will make it harder to learn C++." What am I missing? Perhaps I'm defining my goals wrongly; I'm interested in writing for graphic environments (currently, KDE, but eventually perhaps xfce, gnome, etc.) for Linux (though hopefully in portable enough code to cross-build for Windows and Mac) -- is that way too complicated to do in ANSI C? Will it require an object oriented language? – Zeiss Ikon Nov 07 '14 at 01:29
  • No, you're completely misunderstanding. [Read](http://stackoverflow.com/q/598552/560648). – Lightness Races in Orbit Nov 07 '14 at 10:37
  • I don't have time to read that comment thread in depth right now (need to get dressed for work in 15 minutes), but it appears that there's divided opinion on which to learn first -- yet commenters seem almost unanimous that they're virtually unrelated in terms of actual function, and that C forms a large part of the syntax of C++ (also that C++ is in some kind of middle ground between "procedural" languages like C and genuinely OO languages like C#). I don't get the disadvantage of learning C first. – Zeiss Ikon Nov 07 '14 at 11:15
  • However, last night I found "Learn Python the Hard Way" (free, online instruction book) and started working through it; if I can keep working at it every day, I should be minimally proficient in Python in a matter of weeks. – Zeiss Ikon Nov 07 '14 at 11:17