Thread: Help Please - program to display file renders Windows Program Error

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code:
    int main()
    {
        char filename[20];
        // Allocate a string to store filename - does PC interpret whitespace at end of filename?
        printf("Please enter the filename");
        scanf("%s", &filename);
        // Request name of file to open, store in filename
        FILE *fp;
        fp = fopen(filename,"r");
        // Create file pointer, open file stored in string filename
        char filetext[1024];
        // Create string 1024 cells long to hold text from file
        int i = 0;
        // Compiler doesn't like my fore loops, explicitly create i
        for(i = 0; i <= 1023; i++) {
              filetext[i] = fgetc(filename);
              // Place each character of the file into a different string cell, one after the other
              tolower(filetext[i]);
              // Move through string, converting text to lowercase
              }
        printf("%s",filetext);
        // Display the contents of the file on screen, but...
        if(i == ' ') {
        // If the string cell is blank, i.e a space
             printf("\n");
             // move onto a new line
             }
         }
    Now, let's see what you are doing here:
    Code:
        scanf("%s", &filename);
    You are passing scanf the address to the pointer of the filename buffer.
    Code:
    fp = fopen(filename,"r");
    Doh!
    Code:
        for(i = 0; i <= 1023; i++) {
    filetext[1023] is the last byte of the filetext buffer, so if you don't set the last byte to 0, then you will see a buffer overrun(overread?).
    Code:
              tolower(filetext[i]);
    tolower() returns lowercase filetext[i], but... the return value is not used.
    Code:
        if(i == ' ') {
    If i is 32, it enters that if. (It is always 1024 after the loop)

    Your indentation is buggy (last 2 lines).
    Last edited by maxorator; 11-12-2006 at 11:34 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  4. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  5. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM