Thread: Reading in a file word by word

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    3

    Reading in a file word by word

    I am trying to write a program that reads a file name from the
    command line, reads the contents of that file, and output a list of words, one word per line, to a second file, whose name is also on the command line.


    For this program a word is a sequence of alphabetic characters. Any character which is not alphabetic is a separator character.

    I know how to open files and create files but I am not sure what to do so that I can read in just one word at a time. I also want to know of any tips on how to write to the new file. Thank you in advance for any help!

    Example

    input file:
    Hello%$this is just a test!

    output file:
    Hello
    this
    is
    just
    a
    test

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just continue in whatever book showed you how to open files a page or two, and they'll show you how to read.

    Basicly there are may ways to do it.
    fgets would work.
    fgetc would also work.
    fscanf would also work...

    Basicly, there are a whole lot of f-ing functions that would do the job.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    3

    Open files

    Cool thank you I got it work... I kept forgetting to point to the right spot. Thank you for your help! The only last thing I need to figure out is how to make a new word after a "non letter".

    Example:

    If you use this code:

    Code:
    FILE *fp = fopen(argv[1],"r");
    
    	char buf[10];
    
    		while( fscanf(fp, "%s", buf) != EOF )
    		{
    			printf("%s\n", &buf);
    		}
    On an input file that contains:

    This@#$is just a test!

    Your out put will be
    This@#$is
    just
    a
    test!

    What I need the output to be is:
    This
    is
    just
    a
    test
    Last edited by Bumblebee11; 06-10-2003 at 09:15 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What you would want to do then, is either modify how your fscanf call works, or, use a loop to run through the string you read in, and check for non-alpha characters.

    To do that, something like this would work.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    3

    Got it to work

    I was just able to get my program to work. Thank you for your help!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. multiple file loading. so fruturated! help!
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 05-09-2005, 05:13 PM