File Open Problem - Please provide clues 
Author Message
 File Open Problem - Please provide clues

Hi I'm having problems with opening files which are named  
after simple numbers:                                      
2                                                          
5, etc.                                                    
The reason for naming them this way is because I'm writing a
program in C that increments a number, and then attempts to
open the file that's actually named by that number.        
I get mismatched type errors when using FOPEN:              
 int num;                                                  
 filepointer = FOPEN(num,"r");            or                          
 filepointer = FOPEN("num","r");

Is there a way to manipulate an integer: store it in integer
variables, arrays, etc.., but then use that integer to open
a file actually named by that integer?..                    
 I was going to name the files:                            
  0.txt, 1.txt, 2.txt,                                      
 and so forth, but this was even trickier with the string  
concatenating, (attached ".txt" to it) etc.. so the files  
are named after numbers simply..                            

any help appreciated.                                      
Steve



Thu, 17 Sep 1998 03:00:00 GMT  
 File Open Problem - Please provide clues

Quote:

>Hi I'm having problems with opening files which are named  
>after simple numbers:                                      
>2                                                          
>5, etc.                                                    
>The reason for naming them this way is because I'm writing a
>program in C that increments a number, and then attempts to
>open the file that's actually named by that number.        
>I get mismatched type errors when using FOPEN:              
> int num;                                                  
> filepointer = FOPEN(num,"r");            or                          
> filepointer = FOPEN("num","r");

Please post minimal complete code.  Otherwise, one wonders if
"filepointer" was ever defined.  Was this code placed inside a
function.  Was the stdio.h header included.

There is no FOPEN() function in C.  Why not just use fopen()?

The fopen() function takes two arguments, both of which are
pointers to const char.  In fopen( num, "r" ) you are asking that
an int be treated as a pointer -- what do think the compiler
will do with that?  Your fopen( "num", "r" ) should attempt to
open a file named num.

Here a very simple program that will create a file named 1 (or
rewrite an existing file with that name):

#include <stdio.h>

#define NUMSTRSIZE      10

int main( void )
{
        FILE    *fp;
        int     num = 1;
        char    fn[ NUMSTRSIZE ];

        sprintf( fn, "%d", num );

        if ( fp = fopen( fn, "w" ) )
        {
                /* write to file here with error checking */
                fclose( fp );
        }

        return 0;

Quote:
}

Bill McCarthy

Wendell, NC  USA


Thu, 17 Sep 1998 03:00:00 GMT  
 File Open Problem - Please provide clues

Quote:

> Is there a way to manipulate an integer: store it in integer
> variables, arrays, etc.., but then use that integer to open
> a file actually named by that integer?..
>  I was going to name the files:
>   0.txt, 1.txt, 2.txt,
>  and so forth, but this was even trickier with the string
> concatenating, (attached ".txt" to it) etc.. so the files
> are named after numbers simply..

Convert it to a string with itoa or sprintf.  Then strcat the suffix onto it.
As always, make sure that are ensured you have adequate space reserved for your
array.

--

San Jose, California, U.S.A. && 37 20 07 N 121 53 38 W && the 4th R is respect
H.3`S,3,P,3$S,#$Q,C`Q,3,P,3$S,#$Q,3`Q,3,P,C$Q,#(Q.#`-"C`- && 1love && folasade
Omnia quia sunt, lumina sunt. && Dominion, GIGO, GOOGOL, Omega, Psi, Strategem
"Out from his {*filter*}/his soul went to seek/the doom of the just." -- _Beowulf_



Thu, 17 Sep 1998 03:00:00 GMT  
 File Open Problem - Please provide clues
Hi Steve.

Quote:
> Hi I'm having problems with opening files which are named  
> after simple numbers:                                      
> 2                                                          
> 5, etc.                                                    
> The reason for naming them this way is because I'm writing a
> program in C that increments a number, and then attempts to
> open the file that's actually named by that number.        
> I get mismatched type errors when using FOPEN:              
>  int num;                                                  
>  filepointer = FOPEN(num,"r");            or                          
>  filepointer = FOPEN("num","r");

fopen() accepts as the first argument only a stringpointer. You
have to convert the number in a string.

char buffer[21];
FILE *fp;
int num;

sprintf( (char*)&buffer,"%ld.txt",num);
fp = fopen( (char*)&buffer, "r" );

Quote:
> Is there a way to manipulate an integer: store it in integer
> variables, arrays, etc.., but then use that integer to open
> a file actually named by that integer?..                    
>  I was going to name the files:                            
>   0.txt, 1.txt, 2.txt,                                      
>  and so forth, but this was even trickier with the string  
> concatenating, (attached ".txt" to it) etc.. so the files  
> are named after numbers simply..                            

-----
Andreas Winkelmann




Tue, 22 Sep 1998 03:00:00 GMT  
 File Open Problem - Please provide clues

Quote:

>Hi Steve.

>> Hi I'm having problems with opening files which are named  
>> after simple numbers:                                      
>> I get mismatched type errors when using FOPEN:              
>>  int num;                                                  
>>  filepointer = FOPEN(num,"r");            or                          
>>  filepointer = FOPEN("num","r");

>fopen() accepts as the first argument only a stringpointer. You
>have to convert the number in a string.

>char buffer[21];
>FILE *fp;
>int num;

>sprintf( (char*)&buffer,"%ld.txt",num);
>fp = fopen( (char*)&buffer, "r" );

What is the (char*) cast for?  'buffer' is already a char*.  The name of an
array acts as a pointer to the first element when used as a function argument.

Also, "%ld" is used to print longs - you use "%d" to print an int.

So the above should be:
        sprintf(buffer, "%d.txt", num);
        fp = fopen(buffer, "r");

Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight


TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
or: ftp://ftp-i2.informatik.rwth-aachen.de/pub/arnd/GPS/peter/index.html



Wed, 23 Sep 1998 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Problem opening a file for read while it is open for write

2. Problem with opening binary files using open()

3. CEditView problem: opening a file that is still open

4. Y2ktest- Please Test this source code provided - Y2ktest.zip (0/1)

5. can someone please provide a good explanation about hash tables

6. New tutorial (Interrupts & Handlers) - Please provide feedback

7. Could you please provide code example?

8. Problem with multiselec file in open file dialog

9. can't open file: to many files open

10. Difference between MRU File Open and Regular File Open

11. Please help with opening large files with CDocument::OnOpenDocument

12. Please help with opening large files with CDocument::OnOpenDocument

 

 
Powered by phpBB® Forum Software