Thread: Want to fill a string with text in a file, passing it as parameter to a function

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    1

    Want to fill a string with text in a file, passing it as parameter to a function

    Hi all, i'm trying to make a little program that with a function you can read a file, and get a string (in this case "pStrTrama") fill with the file content. What i see in the debugger is that the string pStr of the function, get fill successfully but it doesn't change the variable "pStrTrama" in main.

    żWhat am i doing wrong?

    Code:
    /*Libraries*/
    #include <stdio.h>
    #include <process.h>
    #include <string.h>
    #include <malloc.h>
    
    
    void getStrFile(char*, FILE*, char*);
    
    
    int main()
    {
        char *pStrTrama = NULL;
        FILE *source = NULL;
    
    
        getStrFile(pStrTrama, source, "IDELCLTSDO_180_IATX.txt");
    
    
        printf("%s", pStrTrama);
    
    
        system("PAUSE");
        return(0);
    }
    
    
    void getStrFile(char *pStr, FILE *pFile, char *pFileName)
    {
        int byte = 0;
        char ch;
    
    
        fopen_s(&pFile, pFileName, "r");
        if (pFile == NULL)
        {
            printf("Can't open source file...\n");
            exit(1);
        }
    
    
        while ((ch = fgetc(pFile)) != EOF)
            byte++;
    
    
        pStr = (char*)calloc(byte, sizeof(char));
    
    
        rewind(pFile);
        for (int x = 0;(ch = fgetc(pFile)) != EOF; x++)
        {
            pStr[x] = (int)ch;
            pStr[x + 1] = '\0';
        }
    
    
        fclose(pFile);
    }

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need a pointer to a pointer for this, otherwise assigning to pStr will only change the local parameter, not the pointer in the caller. Alternatively, you could return the pointer.
    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
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Code:
    while ((ch = fgetc(pFile)) != EOF)
            byte++;
    You could replace this loop with a call to "fseek()", seeking to the end of the file, then using "ftell()" to return the last position in the file, the size of the file. Or you could use "fstat()".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing a file as a function parameter
    By philgrek in forum C Programming
    Replies: 1
    Last Post: 03-06-2011, 01:13 PM
  2. Replies: 1
    Last Post: 11-16-2010, 05:22 PM
  3. Passing an array to a function as a parameter
    By boxden in forum C Programming
    Replies: 1
    Last Post: 03-13-2010, 09:35 AM
  4. Problems passing parameter to function
    By HAssan in forum C Programming
    Replies: 5
    Last Post: 10-21-2008, 02:26 PM
  5. Passing file stream as function parameter
    By simonc2 in forum C++ Programming
    Replies: 6
    Last Post: 12-22-2004, 12:12 PM

Tags for this Thread