?
Solved

Beginner C programming questions

Posted on 2003-02-22
4
Medium Priority
?
265 Views
Last Modified: 2010-04-15
Hi,

  I'm doing a school project. I am not very familiar with C, especially I never program in Unix C with gcc.

Here's my questions:

1. Can the main function's arguments be changed. Instead of int argc, char *argv[], can I change it to something like int k, char flag, char *filename?

2. In my project, the description said, after processing, the output should be written to stdout. What's stdout? Is it the screen? (I'm programming in Unix using gcc)

3. Can someone great in programming C show me some sample code on how to do something like this: Read a input file line by line, breaking each line down to individual words, so I can do some analysis for each line that's broken down to token. Then I write changed code for each line in the input file to the output.

4. A question about strtok function: if I have a string S = "This is a string", then I use lines:
char *word1, *word2, *word3;
word1 = strtok(S, " ");
word2 = strtok(S, " ");
word3 = strtok(S, " ");

would word 1 2 3 has contains the token This; is; a?

Can you please show me some example on how to use strtok or other functions to break down a line of code into tokens that I can examine and use?

Thank you very much.
0
Comment
Question by:rvpals
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Expert Comment

by:Exceter
ID: 8001616
>> 1. Can the main function's arguments be changed. Instead of int argc, char *argv[], can I change it to something like int k, char flag, char *filename?

Sure, define them as anything you want. Why would you want to?

>> 2. In my project, the description said, after processing, the output should be written to stdout. What's stdout? Is it the screen?
>> (I'm programming in Unix using gcc)

Yes, it is the screen.

>> 3. Can someone great in programming C show me some sample code on how to do something like this: Read a input file line by line, breaking each
>> line down to individual words, so I can do some analysis for each line that's broken down to token.
>> Then I write changed code for each line in the input file to the output.

I am afraid that because this is homework I cannot post code that demonstrates this. Basicly all you need to do is read in an entire line of text from file using the fgets() command and run it through strtok().

>> 4. A question about strtok function: if I have a string S = "This is a string", then I use lines:
>> char *word1, *word2, *word3;
>> word1 = strtok(S, " ");
>> word2 = strtok(S, " ");
>> word3 = strtok(S, " ");
>> would word 1 2 3 has contains the token This; is; a?

Yes and no. :-)

word1, word2 and word3 are pointers and therefore contain no data, they mearly point to a memory location that stores it. What will happen is this, strtok() will walk through the input string and replace every instance of each delimiter you specified with a null byte and then returns a pointer to the begining of the current token. Since strtok does not remove these characters and since null bytes are used to terminate strings the pointers word1, word2, and word3 will continue to point to their respective tokens even after strtok() is done tokenizing. This of course renders the input string unreadable so it is advisable to create a copy of the string and pass it to strtok() instead if you wish to use the string again.

An example of strtok(),

/* STRTOK.C: In this program, a loop uses strtok
 * to print all the tokens (separated by commas
 * or blanks) in the string named "string".
 */

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

char string[] = "A string\tof ,,tokens\nand some  more tokens";
char seps[]   = " ,\t\n";
char *token;

void main( void )
{
   printf( "%s\n\nTokens:\n", string );
   /* Establish string and get the first token: */
   token = strtok( string, seps );
   while( token != NULL )
   {
      /* While there are tokens in "string" */
      printf( " %s\n", token );
      /* Get next token: */
      token = strtok( NULL, seps );
   }
}

Exceter
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8001634
>> >> word1 = strtok(S, " ");
>> >> word2 = strtok(S, " ");
>> >> word3 = strtok(S, " ");

Actually, this would have to read,

word1 = strtok(S, " ");
word2 = strtok(0, " ");
word3 = strtok(0, " ");

to achieve the desired result. Otherwise you would only be recieving the first word upon each call because you the pointer to S would force strtok to start at the begining of the string instead of where it left off.

Exceter
0
 
LVL 2

Accepted Solution

by:
Kryp earned 200 total points
ID: 8002445
> 1. Can the main function's arguments be changed. Instead of int argc, char *argv[], can I change it to something like int k, char flag, char *filename?
Not if you expect it to still work.
You can give them different names, but the types and positions are fixed - eg, you can do this
int main ( int numargs, char *arguments[] )

Though argc and argv are the traditional names, and everyone knows what they are when they see them.

Your example may be something like this...
int k = atoi( argv[1] );
char flag = argv[2][0];
char *filename = argv[3];


> Read a input file line by line,
The "copy" function is this

#include <stdio.h>
int main ( ) {
    char    buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, stdin ) != NULL ) {
        fputs( buff, stdout );
    }
    return 0;
}
This copies stdin to stdout

What you do with 'buff' between reading it in and writing it out is entirely up to you.


0
 

Expert Comment

by:skeid21
ID: 8023282
I resently had this exact programing assignment:

I would first recomend going at this through a modular approch and save your functions in a seperate file so that you can use it(if if works good)again with other programs.

I'm sure that the professer gave you a hand out with specifications. like list of functions and what they do. If he did not I would recomend systematically writing down what you need to do and breaking it into different fucntions ex one function that reads from input, another that breaks the input into tokens or fields, another that opens files and intializes your data stucts. and another that closes files.  ***Advice write this modularly**** After writing each different function try it out with test code and see that it works fine then go on to the next part. This will make your life a whole lot easier until you get this thing done. hope i helped
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question