reading from other files one char at a time

This is a discussion on reading from other files one char at a time within the C Programming forums, part of the General Programming Boards category; i need help with: 1) getting a string the user types in and using that string to open an input ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    14

    Angry reading from other files one char at a time

    i need help with:

    1) getting a string the user types in and using that string to open an input file.

    2) reading that input file one character at a time to print out how many times a certain character appears in the input file.

    any help is appreciated. thanks.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >any help is appreciated. thanks.
    I would start by using fgets to read the string, then fopen to open the file. After that I would loop over a call to fgetc until I reached EOF and if I see that certain character, increment a counter. Then I would use printf to show the occurances and top everything off with a call to fclose.

    -Prelude
    My best code is written with the delete key.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    14
    how exactly do you use fgetc and gets? sorry for the stupid questions.

  5. #5
    _Cl0wn
    Guest
    Well fgetc():
    Code:
    #include <stdio.h>
    int main()
    {
        FILE *fd;
        int ch;
        char *filename;
        printf("Enter the text file to open: ");
        fgets( filename, sizeof(filename), stdin);
        if ((fd=fopen(filename, "r"))==NULL){
        perror("fopen");
        return 1;
        }
    
         while ((ch=fgetc(fd)) != EOF)
              putchar(ch);
    return 0;
    }
    That is an example of how to use the two.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    14
    thanks but i have another dumb question. say the file i want to open is C:\file.txt. what would the user have to type in to use that file? i tried run what you showed me to see how it worked and i typed in "file.txt" and it crashed highlighting the 'fgets' line.

  7. #7
    Unreg1
    Guest
    Another classic mistake:
    Code:
    char *filename;
    fgets( filename, sizeof(filename), stdin);
    Where exactly is filename pointing to?

  8. #8
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    And another classic mistake:

    Code:
    fgets( filename, sizeof(filename), stdin);
    if ((fd=fopen(filename, "r"))==NULL){
    Where did you remove the newline character?

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    14
    >>After that I would loop over a call to fgetc until I reached EOF and if I see that certain character, increment a counter.

    how do i do that?

    this is what i have:
    Code:
    int main ()
    {
            char ch[1]; /* User defined character */
            char filename[50]; /* User defined filename */
            int num = 0; /* Number of times character was found */
            FILE *inp; /* pointer to input file */
    
            printf("Enter the name of the file you wish to open: ");
            scanf ("%s", filename);
            inp = fopen(filename, "r");
            if (inp != NULL){
               printf("\nFILE OPEN");
            }else{
               printf("\nNOT OPEN");
               getch();
               exit(1);
            }
            printf("\n\nEnter character you wish to find: ");
            scanf ("%s", &ch);
    
            getch();
            return 0;
    }
    Last edited by jmoney; 02-24-2003 at 02:06 PM.

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    14
    nevermind, i figured it out. thanks to whoever helped.

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Instant buffer overflow code:

    >>char ch[1]; /* User defined character */
    >>scanf ("%s", &ch);

    Don't use scanf() to read a string, it'll only get you in trouble. Read the link I gave you earlier.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. help with reading files
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-24-2002, 10:49 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21