Solved

variable in fopen function?

Posted on 2006-11-13
11
403 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:kgpretty
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17933061
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
 
LVL 6

Expert Comment

by:_iskywalker_
ID: 17933336
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
 
LVL 6

Expert Comment

by:aescnt
ID: 17933376
Don't you mean '\0'? :)
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:kgpretty
ID: 17943050
I tried both, neither worked. What exactly does \0 do?
0
 

Author Comment

by:kgpretty
ID: 17943085
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
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17943129
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
 
LVL 6

Expert Comment

by:aescnt
ID: 17943571
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
 

Author Comment

by:kgpretty
ID: 17949740
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
 
LVL 19

Accepted Solution

by:
VoteyDisciple earned 125 total points
ID: 17949788
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
 

Author Comment

by:kgpretty
ID: 17949910
Thanks VoteyDisciple, your code worked after I added the fflush().
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

815 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now