• C

variable in fopen function?

Can I use a variable in the fopen function. Here's the code:

int x = 0;
char textDoc[255];

printf("Please enter the name of your file. Be sure to include the file extension.");
      
            
while (!(textDoc[x-1] == 't' && textDoc[x-2] == 'x' && textDoc[x-3] == 't' && textDoc[x-4] == '.') && x < 255)
{
      textDoc[x] = getchar();
      x = x + 1;
}
      
printf("%s", textDoc);

tdPtr = fopen(textDoc, "r");



When I manually enter the name of the document into the fopen function, it opens fine.

However when I type it into the textDoc variable as I'd like the program to function, it doesn't open. I know that I entered the correct document name because I printed the contents of textDoc to the screen.

How can I get the file for which the user enters the name, to open?
kgprettyAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
VoteyDiscipleConnect With a Mentor Commented:
Wait, what's with this loop condition:
while (!(textDoc[x-1] == 't' && textDoc[x-2] == 'x' && textDoc[x-3] == 't' && textDoc[x-4] == '.') && x < 255)


I think you meant the following:

int x = 0;
while ((textDoc[x++] = getChar()) != '\n')
    ;
textDoc[x - 1] = '\0';

THEN check for .txt and do anything else you want, but you have to get the whole string first!  You just can't start validating my (i.e. the user's) input before I'm even done entering it.   I don't know whether or not that's contributing to the error you're seeing, but it's definitely worth fixing.
0
 
VoteyDiscipleCommented:
I don't see anywhere you're adding a null terminator to the textDoc string.

You can definitely use a variable to open a file.  Otherwise programs would be pretty hard to use.  (-;
0
 
_iskywalker_Commented:
maybe you should use other function than getchar, since the \0 isnt really surely added. you could do a
char textDoc[256];

int x = 0;

printf("Please enter the name of your file. Be sure to include the file extension.");
     
         
while (!(textDoc[x-1] == 't' && textDoc[x-2] == 'x' && textDoc[x-3] == 't' && textDoc[x-4] == '.') && x < 255)
{
     textDoc[x] = getchar();
     x = x + 1;
}
   textDoc[x]=\0;  
printf("%s", textDoc);

tdPtr = fopen(textDoc, "r");
0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
aescntCommented:
Don't you mean '\0'? :)
0
 
kgprettyAuthor Commented:
I tried both, neither worked. What exactly does \0 do?
0
 
kgprettyAuthor Commented:
Is there a file function that I can use to copy the contents from one file to another? Then perhaps, the content of the user's document may be copied into source.txt.
The file would always be opened using the following:

tdPtr = fopen("source.txt", "r");
0
 
VoteyDiscipleCommented:
In languages like C you're working a lot closer to the contents of memory, which means there's no built-in way to figure out where a string ends.  That is, it's not like a string object in a language like C++ or Java, it's just a bunch of memory.  When any C function processes a string, it does so by reading characters until it sees one with the ASCII value zero -- the null character, which is written '\0'.

And, let me slam really hard on the brakes of the "copy into source.txt" right now.  Copying an entire file just to get around using a variable in a function call is HUGE overkill.  On top of which, how would you tell it which file to copy without passing in a variable name there?

No, adding the null terminator to the string is the right solution; if it didn't work there must have been something quirky in the implementation.  When you say it doesn't work, does that mean it behaves the same as before?
0
 
aescntCommented:
kgpretty, can you provide more info on why exactly it didn't work?

Try using _iskywalker_'s code with the line "textDoc[x]=\0;" changed to "textDoc[x]='\0';".
0
 
kgprettyAuthor Commented:
Here's what I tried:


printf("Please enter the name of your file. Be sure to include the file extension.");

while (!(textDoc[x-1] == 't' && textDoc[x-2] == 'x' && textDoc[x-3] == 't' && textDoc[x-4] == '.') && x < 255)
{
      textDoc[x] = getchar();
      x = x + 1;
}

printf("%s", textDoc);

textDoc[x] = '\0';

tdPtr = fopen(textDoc, "r");
if(tdPtr == NULL)
{
      printf("\ncan't open file"); //exit or return
}
else
{
...............



When I tried to run the program I saw the message "can't open file"
0
 
kgprettyAuthor Commented:
Thanks VoteyDisciple, your code worked after I added the fflush().
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.