Ok, i'm working on a program in C to read in a file given by user and print it out to screen along with its size(for now). I haven't done very much C at all and am still trying to figure out all the pointers and stuff.
Q1. what do you recommend for C debugging? i've heard just using ddd or gdb is the best thing, but i've tried messing around with it and seems quite annoying. any good alternatives?
Here is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char *prog;
int main(int argc, char *argv[])
{
printf("Welcome to the Word Length program\n");
prog=argv[0];
char *inpFile=argv[1];
printf("1");
FILE *inStream=stdin;
if (strcmp(inpFile,"-")!=0)
{
if ((inStream=fopen(inpFile,"r"))==NULL)
{
fprintf(stderr,"%s: <input>\n",prog);
exit(1);
}
}
printf("2");
char *word; //I believe my problem is here
int n=0;
while(scanf("%s",word)!=EOF)//and here
{
printf("2.5");
word=strtok(word," .,;:!()/#");
printf("3");
n=sizeof(word);
printf("The word %s has size %d \n",word,n);
free(word);
}
printf("4");
fclose(inStream);
return 0;
}
The output:
Quote:
Welcome to the Word Length program
12
after that it just sits
q2. I need to use char *word; is there a method for scanning in words with the pointer? i know scanf works with char[20] or w/e size but not with the pointer. any advice?
thanks