Thread: Passing a file pointer to a function is producing undesired output

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    178

    Passing a file pointer to a function is producing undesired output

    Hello everyone,
    I am working on a project and decided to try something simple before I start adding items. I am calling a function from main and that function has a file pointer. Here is my main.cpp
    Code:
    #include <cstdio>
    #include <string>
    #include <iostream>
    #include "main.h"
    
    extern FILE *fp;
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        
        char character;
        
        switch (argc)
        {
            case 1:
                fp = stdin;
                break;
                
            case 2:
                if ((fp = fopen((strcat(argv[1], ".cp")), "r")) == NULL)
                {
                    cout << "Can't open input file!\n";
                    exit (0);
                }
                break;
                
            default:
                cout << "USEAGE: testscanner <filename> OR testscanner\n";
                exit(0);
        }
        
        //while((character = getc(fp)) !=EOF)
        //{
            scanner(fp); 
        //}
        
        fclose(fp);
        
        return 1;
        
    }
    Here is the function:
    Code:
    #include <string>
    #include <cstdio>
    #include <iostream>
    #include "main.h"
    #include "scanner.h"
    
    using namespace std;
    
    void scanner(FILE *fp)
    {
        char nextChar;
    
        while ((nextChar = getc(fp)) != EOF);
        {
            cout << nextChar << endl;       
        }
    
        fclose(fp);
    
    }
    My test file consists of several characters and digits. Nothing special and I at this point in time do not have any type of formatting that needs to be adhered to. I am simply wanting to read the file character by character and print it out. When I run the program, I get this symbol:
    Code:
    ÿ
    If I use a printf statement, such as:
    Code:
    printf("%s\n", nextChar);
    I get a segmentation fault. I know it has to be something really simple that I am missing and need another set of eyes.
    My main.h
    Code:
    #ifndef MAIN_H
    #define MAIN_H
    
    void scanner(FILE *);
    //char getChar(FILE *);
    
    #endif
    and lastly my scanner.h
    Code:
    #ifndef SCANNER_H
    #define SCANNER_H
    
    FILE *fp;
    
    #endif
    I hope someone can show me what I need to do in order to correct this. Thank you everyone.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why are you using FILE instead of say, std::fstream?

    Quote Originally Posted by csharp100
    If I use a printf statement, such as:
    Code:
    printf("%s\n", nextChar);
    I get a segmentation fault.
    nextChar is a char, not a null terminated string, yet you are trying to print it as a null terminated string.

    Unless you have special requirements, make use of the C++-style I/O functionality instead. You already did so with:
    Code:
    cout << nextChar << endl;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Define "FILE *fp;" inside main (get rid of "extern").


    Do the "strcat" into a separate char array (you cannot do it safely into argv[1]).


    And main is supposed to return 0 (or, equivalently, EXIT_SUCCESS) if it succeeds, and EXIT_FAILURE (or, less portably, non-zero) on failure.

    EDIT: I should add (after reading laserlight's post) that you're not really writing in C++ or C, but in the monstrosity known as "C with cout".
    Last edited by oogabooga; 10-27-2013 at 09:01 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by laserlight View Post
    Why are you using FILE instead of say, std::fstream?


    nextChar is a char, not a null terminated string, yet you are trying to print it as a null terminated string.

    Unless you have special requirements, make use of the C++-style I/O functionality instead. You already did so with:
    Code:
    cout << nextChar << endl;
    Code:
    printf("%c\n", nextChar);
    Produces the same results.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That means that you have a bug elsewhere in your code, e.g., the fact that getc returns an int, not a char, yet you store its return value in a char.

    Rewrite your program to use C++-style I/O streams then post your updated program if you still have problems.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by laserlight View Post
    Rewrite your program to use C++-style I/O streams then post your updated program if you still have problems.
    @csharp,
    Or come over to the dark side and post in the C forum.
    Did you try making the changes I mentioned?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by oogabooga View Post
    @csharp,
    Or come over to the dark side and post in the C forum.
    Did you try making the changes I mentioned?
    yes and the problem was a semicolon at the end of the while loop in the scanner code. I inadvertently added one. Habit of the "pinkey" finger.
    Last edited by csharp100; 10-29-2013 at 09:33 AM. Reason: better explanation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-14-2013, 08:33 PM
  2. Code giving undesired output..
    By somali.cc in forum C Programming
    Replies: 7
    Last Post: 10-06-2012, 11:38 AM
  3. passing a file pointer to a function
    By estefany in forum C Programming
    Replies: 4
    Last Post: 08-02-2012, 08:03 PM
  4. Random Walk Simulation in 1D - Undesired output
    By SalamuB in forum C Programming
    Replies: 2
    Last Post: 01-30-2012, 07:03 AM
  5. structure woes equals undesired output
    By skeptik in forum C Programming
    Replies: 3
    Last Post: 07-23-2004, 02:20 AM