?
Solved

File names???

Posted on 2000-01-12
2
Medium Priority
?
225 Views
Last Modified: 2010-04-15
I'm starting programming in C, and would like to write the program in such a way that I can open any file, rather than one specified in the middle of the code.

I can't work out how to get the file name specified after the line

printf("Please enter your file name");
scanf("%c", &name);

to then be used to open the file in the line

fprintf("[needs to point to name]", "a");

(I want to write data to the end of the file hence a for append)

Any help would be greatly appreciated
0
Comment
Question by:Nile_6670
2 Comments
 

Author Comment

by:Nile_6670
ID: 2345133
Edited text of question.
0
 
LVL 18

Accepted Solution

by:
deighton earned 60 total points
ID: 2345152
You don't use fprintf quite like this, you need to get a file pointer to the file  using fopen

#include <stdio.h>

void main()
{
    FILE *fp;
    char name[80];

printf("Please enter your file name");
                      scanf("%c", &name);

    if((fp = fopen(name,"w"))==NULL) {
        printf("failed to open file tfile");
        return;
    }
    else {
            fclose(fp);
            printf("closed file tfile");
        }
fprintf(fp,"Test");
fclose(fp);
    return;
}
0

Featured Post

Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question