Maximum PC

It is currently Sun Apr 26, 2015 2:58 pm

All times are UTC - 8 hours

Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: C program help reading file from stdin
PostPosted: Mon Mar 15, 2010 5:34 pm 

Joined: Thu Jul 08, 2004 6:46 am
Posts: 73
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:
#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");

   char *inpFile=argv[1];
   FILE *inStream=stdin;

   if (strcmp(inpFile,"-")!=0)
      if ((inStream=fopen(inpFile,"r"))==NULL)
      fprintf(stderr,"%s: <input>\n",prog);

   char *word; //I believe my problem is here
   int n=0;
   while(scanf("%s",word)!=EOF)//and here
      word=strtok(word," .,;:!()/#");
   printf("The word %s has size %d \n",word,n);

   return 0;

The output:
Welcome to the Word Length program

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?


 Post subject:
PostPosted: Mon Mar 15, 2010 7:50 pm 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4745
Location: In the monkey's litterbox
On Windows, Visual Studio has the best debugger I've ever used. On *nix, I'd just use gdb -- it's not that hard to use.

What OS are you running on? I got the code to execute to completion as is by entering ^D (aka EOF) on stdin under x86_64 Linux. However, when I went to enter some input, it failed at the strcmp because word == NULL.

What you need to do is read in a char[BUFFER_SIZE], process that, and then get the next buffer worth of characters until the end of the file. BUFFER_SIZE should be a number of characters, usually at least 80.

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 8 hours

Who is online

Users browsing this forum: No registered users and 3 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.