Solved

variable in fopen function?

Posted on 2006-11-13
11
402 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
 

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
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

863 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

22 Experts available now in Live!

Get 1:1 Help Now