Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

variable in fopen function?

Posted on 2006-11-13
11
Medium Priority
?
409 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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.

705 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