C Problem

This is a discussion on C Problem within the C Programming forums, part of the General Programming Boards category; I need help on writing a program, this was the breif: "Write a program to count the number of characters ...

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

    C Problem

    I need help on writing a program, this was the breif:

    "Write a program to count the number of characters and lines in a text file. In addition, determine the percentage of characters of the following classes: digit, upper-case letters and lower case letters, angled and curly bracket.

    Create a test file with some (at least 7) lines of text, containing different mixes of characters and digits to test the program.

    Prompt the user for the filename and store the results in a different file. The results should be stored in a neat and easy to read format, with a proper heading including the name of the file tested"

    I can write programs to count the number of characters in a string and I can write a program that will open a file. What I cant do is to put these two together in any useable way. Any help on the problem, would be much appreciated.

    Thanks

  2. #2
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    Post your code for what you have done(using code tags) and someone will help you!!

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    2
    This is the code that should open a document but I cant get it to work at the moment

    "[File: copy/copy.c]
    #include <stdio.h>
    const char FILE-NAME[] = "input.txt";
    #include <stdlib.h>

    int main()

    {

    int count = 0; /*number of characters seen*/
    FILE *in_file; /*input file*/

    /*character of EOF flag from the input*/

    int ch;

    in_file = fopen(FILE_NAME, "r");
    if (in_file == NULL) {
    printf("cannot open %s\n", FILE_NAME);
    exit(8);

    }

    while (1) {
    ch = fgetc(in_file);
    if (ch == EOF)
    break;
    ++count;

    }

    printf ("Number of characters in %s is %d\n", FILE_NAME, count);

    fclose(in_file);
    return (0);

    }

    "

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    Code:
    const char FILE-NAME[] = "input.txt";
    in_file = fopen(FILE_NAME, "r");
    Two different variables for one thing...you're asking for something that doesnt even exist. I think FILE-NAME is an illegal way to name a variable too. The '-' part is.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    Code:
    const char FILE_NAME[] = "c:\\input.txt";
    I did this and it works fine. Try puting the whold directory in for it. input.txt should work as long as you put it in the right place but with the directory there you cant go wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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