Solved

variable in fopen function?

Posted on 2006-11-13
11
401 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.

760 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

21 Experts available now in Live!

Get 1:1 Help Now