Thread: Read word from text file (It is an essay)

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    Unhappy Read word from text file (It is an essay)

    I am a AD students. I have a question here. does anyone help me?

    i am writing a program to get word from text file, which is an eassy. i need to sperate each word and then write to another text file. in the new text file, the word should be one word per line , no numbers or other char such as !, ? and many others, no double or something like that are allow.

    i have wrote it, but something is wrong. I get word from text and write to the new text file but in the new text file. i have find the double line. Why does it cause? What's wrong is that?

    for examples
    in the text file
    ***************
    i am a boy. i am 15 years old.
    **************
    in the new text file

    i
    am
    a
    boy
    i
    am
    years
    old

    my code is here
    Code:
    #include<stdio.h>
    
    void main()
    {
    	FILE *fp,*fp1;
    	char word[1000000];
    	int i=0;
    	
    	fp=fopen("wap.txt","r");
        fp1=fopen("wordlist.txt","w");
    
    	while (!feof(fp))
    	{
          fscanf(fp,"%c",&word[i]);
    	  if(word[i] == ' ' || word[i]=='\n')
    	  {
    	  fprintf(fp1,"%c\n",word[i]);
    	  }
    	  else if (((word[i]>='a') && (word[i]<='z')) || ((word[i]>='A') && (word[i]<='Z')))
    	  {
    		  fprintf(fp1,"%c",word[i]);
    	  }
       
    		
    	}
    
    	fclose(fp);
    	fclose(fp1);
    	
    
    }
    Last edited by forfor; 05-07-2003 at 12:46 PM.

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Am I understanding your question properly? Do you mean that for the text file
    i am a boy. i am 15 years old.
    the output file should be:
    i
    am
    a
    boy
    years
    old

    the second "i am" is removed?

    If so, you might want to keep the list of words you found so far in memory so that each word from the file can be compared to the 'already found' list. the string function strstr() can help with this.

    Walt

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>no numbers or other char such as !, ?
    Lookup isalpha()

    >>double line
    This is caused by your incorrect use of feof(). Read this:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Try this:
    Code:
    void main()
    {
      FILE *fp,*fp1;
      char c;        /* the word array was never fully used */
      	
      fp=fopen("wap.txt","r");
      fp1=fopen("wordlist.txt","w");
    
      while ((c = fgetc(fp)) != EOF)    /* as per Hammer's post */
      {
        if(c == ' ' || c=='\n')
        {
          fprintf(fp1,"\n");   /* <-- changed this line */
        }
        else if (((c>='a') && (c<='z')) || ((c>='A') && (c<='Z')))
        {
          fprintf(fp1,"%c",c);
        }		
      }
    
      fclose(fp);
      fclose(fp1);
    }
    before, when you read a '\n' you were replacing it with "\n\n".

    Does that fix your problem?
    Last edited by DavT; 05-08-2003 at 06:27 AM.
    DavT
    -----------------------------------------------

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    Thx!! DavT

    but the problem is still here

    for example, the input file
    I am a boy.


    i am 15 years old

    Output file is

    I
    am
    a
    boy



    i
    am

    years
    old

    my problem is to skip the line space like this

    output file
    I
    am
    a
    boy
    i
    am
    years
    old

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Use some variable to keep track of lines that received alphabetic character(s) already. Only add a newline if alphabetic characters have preceded it.
    Code:
    #include<stdio.h>
    #include<ctype.h>
    
    int main(void)
    {
       FILE *infile  = fopen("wap.txt","r");
       FILE *outfile = fopen("wordlist.txt","w");
       if ( infile != NULL && outfile != NULL )
       {
          int terminate = 0;
          for ( ;; )
          {
             int ch = fgetc(infile);
             if ( ch == EOF )
             {
                break;
             }
             if ( isalpha(ch) )
             {
                fputc(ch, outfile);
                terminate = 1; /* okay to add newline on next space */
             }
             else if ( isspace(ch) && terminate )
             {
                fputc('\n', outfile);
                terminate = 0;
             }
          }
          fclose(infile);
          fclose(outfile);
       }
       return 0;
    }
    
    /* wap.txt
    i am a boy. i am 15 years old.
    */
    
    /* wordlist.txt
    i
    am
    a
    boy
    i
    am
    years
    old
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    Dave_Sinkula

    Thx!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    the problem has been solved

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  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. Read a text file and respond
    By Arangol in forum C++ Programming
    Replies: 5
    Last Post: 07-12-2006, 04:37 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM