Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++

Name: Anonymous 2011-05-24 22:28

Write a program that reads a text file and counts and displays the number of characters(with whitespaces), number of characters(without whitespaces), number of words and number of lines in the text file.

plz help me i dont have any idea of filing

Name: Anonymous 2011-05-24 22:30

You should switch to IT.  No, that's too much for you.  MIS.

Name: Anonymous 2011-05-24 22:31

>>1
This seems to me like a homework question. We will not do your homework for you, just because you will learn nothing when we do.
I doubt that you have been given this task before being instructed how to do that. So you would better attend to your classes, then you should be able to do that. At least you should come up with a basic idea how to do that.
If you come up with what you have already done and where you have difficulties in your program, there will be many members here that will try to help you, but at first you have to show us your effort.

Name: Anonymous 2011-05-24 22:39

     /* Sample implementation of wc utility. */
    
     #include <stdlib.h>
     #include <stdio.h>
     #include <stdarg.h>
    
     typedef unsigned long count_t;  /* Counter type */
    
     /* Current file counters: chars, words, lines */
     count_t ccount;
     count_t wcount;
     count_t lcount;
    
     /* Totals counters: chars, words, lines */
     count_t total_ccount = 0;
     count_t total_wcount = 0;
     count_t total_lcount = 0;
    
     /* Print error message and exit with error status. If PERR is not 0,
        display current errno status. */
     static void
     error_print (int perr, char *fmt, va_list ap)
     {
       vfprintf (stderr, fmt, ap);
       if (perr)
         perror (" ");
       else
         fprintf (stderr, "\n");
       exit (1); 
     }
    
     /* Print error message and exit with error status. */
     static void
     errf (char *fmt, ...)
     {
       va_list ap;
      
       va_start (ap, fmt);
       error_print (0, fmt, ap);
       va_end (ap);
     }
    
     /* Print error message followed by errno status and exit
        with error code. */
     static void
     perrf (char *fmt, ...)
     {
       va_list ap;
      
       va_start (ap, fmt);
       error_print (1, fmt, ap);
       va_end (ap);
     }
    
     /* Output counters for given file */
     void
     report (char *file, count_t ccount, count_t wcount, count_t lcount)
     {
       printf ("%6lu %6lu %6lu %s\n", lcount, wcount, ccount, file);
     }
    
     /* Return true if C is a valid word constituent */
     static int
     isword (unsigned char c)
     {
       return isalpha (c);
     }
    
     /* Increase character and, if necessary, line counters */
     #define COUNT(c)       \
           ccount++;        \
           if ((c) == '\n') \
             lcount++;
    
     /* Get next word from the input stream. Return 0 on end
        of file or error condition. Return 1 otherwise. */
     int
     getword (FILE *fp)
     {
       int c;
       int word = 0;
      
       if (feof (fp))
         return 0;
          
       while ((c = getc (fp)) != EOF)
         {
           if (isword (c))
             {
               wcount++;
               break;
             }
           COUNT (c);
         }
    
       for (; c != EOF; c = getc (fp))
         {
           COUNT (c);
           if (!isword (c))
             break;
         }
    
       return c != EOF;
     }
          
     /* Process file FILE. */
     void
     counter (char *file)
     {
       FILE *fp = fopen (file, "r");
      
       if (!fp)
         perrf ("cannot open file `%s'", file);
    
       ccount = wcount = lcount = 0;
       while (getword (fp))
         ;
       fclose (fp);
    
       report (file, ccount, wcount, lcount);
       total_ccount += ccount;
       total_wcount += wcount;
       total_lcount += lcount;
     }
      
     int
     main (int argc, char **argv)
     {
       int i;
      
       if (argc < 2)
         errf ("usage: wc FILE [FILE...]");
      
       for (i = 1; i < argc; i++)
         counter (argv[i]);
      
       if (argc > 2)
         report ("total", total_ccount, total_wcount, total_lcount);
       return 0;
     }


number of characters(without whitespaces)
exercise, reader, etc

Name: Anonymous 2011-05-24 22:40

>>1
This is pretty basic stuff. Why don't you actually try for once?

Name: Anonymous 2011-05-24 22:40

>>3
members
/prog/ is some super seekrit club, amirite?

Name: Anonymous 2011-05-24 22:43

static int [unnecessary line break]
isword(unsigned char c)

...

Stop that.

Name: Anonymous 2011-05-24 22:45

Google the following:

fopen
fwrite
fread
fseek
fclose

Name: Anonymous 2011-05-24 22:52

Write a program
I'm curious about when your course started.

How long have you been studying C++ to reach this level of no idea?

If your courses run the same schedule as my classes did, you probably have waited too long before putting in the first bit of effort. I don't think you can pass the course.

But if you put in some effort and you ask a specific question (about part of the assignment), I'll probably give some constructive answer. Meanwhile, since you put C++ in your thread title, ignore >>2-san's answer above. C I/O functions work in C++, but I'm sure that is not what your instructor wants.

Name: Anonymous 2011-05-24 22:53

>>9
I meant >>8, not >>2. What the fuck was I smoking?

Name: Anonymous 2011-05-24 23:02

>>10
You were smoking LOTS AND LOTS OF COCKS

Name: Anonymous 2011-05-24 23:17

>>11
chocks

Name: Anonymous 2011-05-24 23:53

>>12
C++0x

Name: Anonymous 2011-05-24 23:55

Just remember that an object is an instance of a class. You should also understand inheritance and be able to pronounce ``Bjarne Stroustrup''. Once you have mastered these few basic concepts, you should have no trouble writing your program.

I never got past inheritance, but here is the general idea:

Declare and initialize variables for the things that you want to count. You will also need a character array.

Open file for reading.

Read line of text into your character array. C++ should have some kind of handy function that will read a single line of text from a file. It is up to you to find out what it is. Half the fun is pawing through manuals looking for functions that do what you want.

After you have read your line of text, you can increment your line counter.

Step through your character array, test whether or not each element is a white space, and increment the appropriate counter. Are there any other white spaces besides tab and blank? A word is one or more non white space characters, followed by any number of white spaces, or an end of line provided that the last character wasn't a hyphen. Are hyphenated words one or two words?

When you have finished reading lines and counting things, you can close your file, do any needed math and print out your results.

There are a few nit-picking little details that I haven't mentioned, but this should be enough to get you started.

Name: Anonymous 2011-05-25 0:02

I never got past inheritance
And I stopped reading there.

Name: Anonymous 2011-05-25 0:40

BEEEEEEEEEEEEEEEEEEEEEEYAAARRRRRRRRNN

Name: Anonymous 2011-05-25 0:46

system("wc")

Name: Anonymous 2011-05-25 2:07

Name: Anonymous 2011-05-25 3:18

Isn't he a member of ABBA?

Name: Anonymous 2011-05-25 3:43

>>18
Beeyarneh... Skgogstup?

Name: Anonymous 2011-05-25 4:08

Name: Anonymous 2011-05-25 4:39

>>21
zomgoptimized

Name: Anonymous 2011-05-25 4:49

>>21
That indentation hurts my eyes. Really, just one space?

Name: Anonymous 2011-05-25 4:53

>>23
Less spaces means less time for parsing the file. Everyone wins.

Name: Anonymous 2011-05-25 5:00

>>21
ANONIX QUALITY

Name: Anonymous 2011-05-25 5:08

this can be written easily and shorter using flex

Name: Anonymous 2011-05-25 6:36

>>7
That's actually K&R style or something archaic.

Name: Anonymous 2011-05-25 7:30

>>26
Even shorter using Perl:

# boilerplate omitted
$chars = +($contents.comb);
$chars_nows = +($contents.comb: /\w/);
$words = +($contents.comb: /\w+/);
$lines = +($contents.comb: /\V+/);

Name: Anonymous 2011-05-25 8:24

>>23
Your eyes need a check or your font is too small if you can't see the indents.

Name: Anonymous 2011-05-25 9:06

>>29
He CAN see it.  It just hurts.

Name: Anonymous 2011-05-25 17:04

You can see my dick, and it hurts you.

Name: Anonymous 2011-05-25 17:06

Hmmm. I wonder how something like this would work.

#include <fstream>
#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
    {
        if (argc == 1)               //quit if no arguments
        {
            cerr << "Usage: " << argv[0] << " filenames\n";
            exit(1);
        }

        ifstream fin;                 // open stream
        long count;
        long whitespace;
        long linecount = 0;
        long wordcount = 0;
        long total = 0;
        char ch;
        int newword;
        char tab = 9;
        char blank = ' ';

        for (int file = 1; file < argc; file++)
        {
            fin.open(argv[file]);
            count = 0;
            whitespace = 0;

            linecount = 0;
            newword = 0;
            wordcount = 0;
            while (fin.get(ch))
            {
                count++;
                if (ch == blank || ch == tab || ch == '\n')
                {
                    whitespace++;
                    if (newword == 1)
                    {
                        newword = 0;
                        wordcount++;
                    }
                }
                else
                {
                    newword = 1;
                }    
        if (ch == '\n')
            linecount++;
            }

            cout << "\n" << "File name = " << argv[file] << "\n";
            cout << "Characters = " << count << "\n";
            cout << "Whitespace characters =  " << whitespace << "\n";
            cout << "Lines = " << linecount << "\n";
            cout << "Words = " << wordcount << "\n";
            fin.close();
         }


        exit(0);
    }

Name: Anonymous 2011-05-25 20:42


#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
    FILE *file;
    unsigned int cc = 0, cw = 0, cl = 0;
    int b = 0, c;

    if (argc != 2)
        return EXIT_FAILURE;
    if (!(file = fopen(argv[1], "r"))) {
        perror("Could not open file");
        return EXIT_FAILURE;
    }

    while ((c = fgetc(file)) != EOF) {
        cc++;
        if (isspace(c)) {
            if (c == '\n')
                cl++;
            if (b)
                cw++;
            b = 0;
        } else {
            b = 1;
        }
    }

    if (b)
        cw++;

    fclose(file);

    printf("chars: %u\nchars without whitespaces: %u\nwhitespaces: %u\nlines: %u\n",cc, cc-cw, cw, cl);
    return EXIT_SUCCESS;
}

Name: Anonymous 2011-05-25 21:34

sdf;sdf
is that a single word in any of your solutions?

Name: Anonymous 2011-05-26 0:02

Is FIOC expanded to Forced Indentation of Code in any of your solutions?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List