Thread: Where do I start?

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    6

    Where do I start?

    I'm in an introduction to C Programming class, and I'm completely lost!

    I am trying to write a program that takes words from a text file and prints each one on a seperate line of an output file followed by the number of letters in the word. Any leading or trailing punctuation must be left out. When all the text has been processed, I have to display on the screen a count of the words in the file.

    I am not looking for someone to write this program out for me, but where do I get started? I know nothing about fopen...? This is frustrating me. Please help as much as possible! TIA

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I am trying to write a program that takes words from a text file and prints each one on a seperate line of an output file followed by the number of letters in the word. Any leading or trailing punctuation must be left out. When all the text has been processed, I have to display on the screen a count of the words in the file.
    This is in an introductory class? How far into it are you? The way you're talking suggests that you've been thrown to the wolves after a few days, but the above assignment wouldn't be given until you knew enough to actually write it. That suggests that you slacked off in class, didn't listen, and now you're in over your head.

    >but where do I get started?
    I usually start with this:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      return 0;
    }
    >I know nothing about fopen...?
    Do you have a book? I don't know of a book on C that doesn't give you enough information to work with files.
    My best code is written with the delete key.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    fopen takes two arguments. The first one is the filename, the second one the mode. Among other things, the mode can be "r" to read a file (which will fail (ie, return NULL (0)) if the file doesn't exist), and "w" to write to a file (beware that this truncuates the file, ie, if it exists, it's deleted).

    fclose() should be called on a file when you're done with it.

    Example:
    Code:
    #include <stdio.h>
    
    int main(void) {
        if((fp=fopen("filename.ext", "r")) == NULL) {
            printf("Can't open file. It may not exist.\n");
        }
        else {
            printf("File was opened.\n");
            fclose(fp);
        }
    }
    Note that you could read the filename from the user (not by using gets(), though).

    Once a file has been opened, you can read from it with fgetc and fgets. fgetc (or getc, which is a macro implementation of fgetc) reads a single character from a file, and fgets returns a string. fgetc return EOF if the end of the file has been reached (EOF = End Of File).

    Code:
    void read_from_file(FILE *fp) {
        int c;  /* must be an int to hold EOF */
    
        while((c = fgetc(fp)) != EOF) printf("%c", c);
    }
    Read a C book for more information. Or search google. (That's how I got all those links.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    6
    Quote Originally Posted by Prelude
    This is in an introductory class? How far into it are you? The way you're talking suggests that you've been thrown to the wolves after a few days, but the above assignment wouldn't be given until you knew enough to actually write it. That suggests that you slacked off in class, didn't listen, and now you're in over your head.
    I'm definately in over my head! I'm glad I changed from CS to Industrial Engineering, I couldn't handle four more years of stuff like this. I love math but this is beyond me.
    I don't slack off in class, The professor just goes too fast for me. She goes through slides and doesn't explain it enough. So, I then have to spend time teaching myself. I've been doing pretty well so far: 100's on the projects, and an 85 and 86 on the two tests, but I just found out I have an assignment due tomorrow. Ignorance/poor planning on my part... I know.

    Most of the people in that class understand what they are doing... so I guess I'm the minority. I once said "I regret taking this class." and everyone thought I was crazy... "This class is easy!" they say.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can use the isalpha() function in <ctype.h> to see if a character is a letter. (There are also corrosponding functions isdigit(), islower(), and isupper() (for upper and lowercase).)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    looky looky
    mm...just a note, in the above example, fp was never declared. so
    Code:
    int main(void) {
        if((fp=fopen("filename.ext", "r")) == NULL) {
            printf("Can't open file. It may not exist.\n");
        }
    ....
    should be more like
    Code:
    int main(void) {
    FILE *fp;
        if((fp=fopen("filename.ext", "r")) == NULL) {
            printf("Can't open file. It may not exist.\n");
        }
    ....
    Registered Linux User #380033. Be counted: http://counter.li.org

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Whoops.

    Can you try to write your program now?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >She goes through slides and doesn't explain it enough.
    This is probably the most common problem in a classroom environment. You're there to learn and your teacher is there to teach you. If you don't understand something, raise your hand and ask for clarification. I'd say that half of the people who have problems simply didn't ask questions that they should have. The other half are lazy bums.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    You're lucky to have a professor who has a grasp of the english language well enough for you to at least understand the words that are coming out of her mouth. Always remember that your professors have PhDs and probably have been working in these areas for years and most of the time take for granted that they are teaching people who have little or no programming experience.
    And, don't think that you're going to escape programming by switching majors, any engineering school worth it's salt will require you to take a programming course, it will continue to haunt you throughout your career. Trust me, I graduated with a degree in Nuclear Engineering/Engineering Physics and all i do all day is write code and software to facilitate my calculations.
    Stick with it and you'll get it eventually. If you aren't using it, try and pick up the programming book by Dietel and Dietel for whatever language you are learning. They always explain things throughly and with the assumption that you know 0!

  10. #10
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    I think the C programming class i took used a book by Dietel... it was called "C How to Program" and it had a section for C, C++, and then a smaller one for Java. except the only time i opened the book in the class was when we had to type out one of the examples and modify it a little ;]
    but my instructor liked the book a bit.
    Registered Linux User #380033. Be counted: http://counter.li.org

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >take for granted that they are teaching people who have little or no programming experience
    And in many cases, the professors also have no programming experience. It's at the point where employers can be wary of a PhD because it suggests you have book knowledge but no real practical experience. There's a huge difference between classroom programming and real world programming.
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    6
    Thanks for all the feedback... I think I've got most of it figured out.

    As far as the class goes... My professor is brilliant when it comes to programming. She knows everything very well, but I just feel she doesn't know how to transmit her information to us. She is so much better at programming than we are, it's nearly impossible to teach us what she knows. I, almost, would rather have someone who didn't know as much, as long as they explained things in a way I could understand.

    The same thing goes for the math department at my school... it depends on the professor. My college algebra professor was brilliant but didn't make sense and I got a B. My pre-calculus professor is brilliant but can teach in a way everyone understands and I have a solid A. And I know my algebra professor didn't just effect me, nearly everyone felt the same way.

    Anyways, I just wanted to rant for a second. I'll stick it out and figure out what I'm doing. Thanks for the input.

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    6
    Ok, this is what I've got so far... But this seems to only be half of what I need.
    I have not figured out how to display the words on seperate lines, or count the number of letters and number of words... any help perhaps?????

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdafx.h>
    
    #define size 65
    
    int main(void)
    {
    	char input[size],
    		output[size];
    	char a;
    	int words=0, end=1;
    	FILE *inp,
    		*outp;
    
    	printf("Enter read file\n");
    	for(scanf("%s", input); (inp=fopen(input, "r"))== NULL; scanf("%s", input))
    	{
    		printf("Can't open %s\n", input);
    		printf("Re-enter file name>  ");
    	}
    
    	printf("Enter write file\n");
    	for(scanf("%s", output); (outp=fopen(output, "w"))== NULL; scanf("%s", output))
    	{
    		printf("Can't open %s\n", output);
    		printf("Re-enter file name>  ");
    	}
    	a=getc(inp);
    
    	while(a != EOF)
    	{
    		if(isalpha(a) || isdigit(a))
    		{
    			putc(a, outp);
    			end=1;
    		}
    		else if(isspace(a) && end)
    		{
    			end=0;
    			fprintf(outp, "n");
    			words++;
    		}
    		a=getc(inp);
    	}
    	printf("\nThere were %d words.", words);
    	fclose(inp);
    	fclose(outp);
    	return(0);
    }

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    6
    Can anyone help me put this together?

    Code:
    	a=getc(inp);
    	int mycounter=0;
    
    	while(a != EOF)
    	{
    		if(isalpha(a) || isdigit(a))
    		{
    			putc(a, outp);
    			end=1;
    			mycounter++;
    		}
    		else if(isspace(a) && end)
    		{
    			end=0;
    			fprintf(outp, "n");
    			words++;
    		}
    		putc( what goes here?)
    		//*a=getc(inp);*//
    	}
    	printf("\nThere were %d words.", words);
    	printf("\nThere were %d letters.", mycounter);
    I added the "mycounter" to keep track of the letters, and I added a few other things in bold. Now I need to display the words on seperate lines in the output file... I asked the professor, and she told me to put in the putc( part, but I forgot what I'm supposed to add!!? Any ideas?
    Last edited by RavUnRex; 11-17-2005 at 05:16 PM.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    C How to Program is an excellent book.

    putc.

    BTW, you might not want to call your variable inp. inp is a macro on some compilers. (Like DJGPP.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  3. C++ gui for windows where to start
    By prixone in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2006, 11:48 PM
  4. GNOME Desktop won't start (Mandriva)
    By psychopath in forum Tech Board
    Replies: 10
    Last Post: 07-19-2006, 01:21 PM
  5. Start bar color in WinXP
    By confuted in forum Tech Board
    Replies: 4
    Last Post: 05-03-2003, 06:18 AM