Thread: change file mode

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    180

    change file mode

    I made a function that receives a file pointer. That file pointer was set in write mode from main. How can i change it inside the function to read mode now? If i use fopen or freopen i have to give filename but don't we already have that information once the function receives the FILE pointer as argument? I don't want to create a new parameter just for this.
    Code:
    void ListStuff(FILE *f)
    {
    //How do i turn f into read mode here?
    
    }
    
    int main()
    {
        
        
        char fileName[50];
        printf("file name? ");
        scanf("%s", fileName);
        char auxFile[50];
        sprintf(auxFile, "%s.txt", fileName);
        FILE *f;
        f=fopen(auxFile, "w");
        if(f==NULL)
        {
            printf("Error\n");
            return 0;
        }
        ListStuff(f);

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can use "w+" instead of just "w" to be able to also read the file.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 07-17-2011, 08:48 AM
  2. how to change background color in console mode
    By smore in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2003, 01:44 PM
  3. Replies: 3
    Last Post: 09-26-2002, 11:07 AM
  4. How to change a mouse cursor in console mode
    By GaPe in forum Windows Programming
    Replies: 10
    Last Post: 07-03-2002, 07:42 AM