line 33: buffer is a character array, therefore it will never be < 0 and that while loop makes absolutely no sense.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
argc contains the number of command-line arguments. in your example it will be 3, the filename + 2 arguments = 3. If you want the number of characters in the arguments just run through argv and count them.
int main(int argc, char* argv[])
{
int counter = 0;
int i;
for(i = 0; i < argc; ++i)
counter += strlen(argv[0]);
printf("The number of characters is %d\n", counter);
return 0;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>>this does not work, what about white spaces
The command interpret ignores all white space (except to separate the actual parameters), so programs don't see them. These are all the same to your program, they both produce the same results.
./a02 23 3
./a02 23 3
>>says the count is one--- needs to be three
The value of argc should be 3 in the example you posted. You probably did not test the program correctly.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I would think that would be pretty obvious if you wrote all that code yourself. This will send the entire string to writer() -- you should lete writer() worry about how to send the characters through the pipe.
else {
FILE* stream;
close (fds[0]);
stream = fdopen (fds[1], "w");
for(i = 1; i < argc; i++)
{
writer (argv[i], 1, stream);}
}
close (fds[1]); // not sure about the placement of this line.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
actually writer() coulld be simplified like this: (removing one parameter)
void writer (const char* message, FILE* stream)
{
fprintf (stream, "%s\n", message);
fflush (stream);
}
But if the program on the receiving end needs all the arguments including spaces, then you can easily construct a single string to represent that
int main(int argc, char **argv)
{
int i;
char buffer[255] = {0};
for(i = 1; i < argc; i++)
{
strcat(buffer, argv[i]);
strcat(buffer, " ");
}
...
...
writer(buffer, stream);
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343