GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Jul-2005, 06:37
likeit likeit is offline
New Member
 
Join Date: Jul 2005
Posts: 11
likeit is on a distinguished road

standard I/O : fopen()


hi,i want to ask bout fopen()in thecode :
CPP / C++ / C Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

#define TIMESIZE 176555L
#define FILESIZE '#'
#define FILETIME '&'

void DisplayCommaDelim(long fsize)
{
long parts[6];
int p;

p=0;
while(fsize>0)
{
parts[p]=fsize % 1000;
fsize/=1000;
p++;
}

printf("%3d",parts[--p]);
while(p>0)
{
printf(",%03d",parts[--p]);
}
}

void displaySize(char *timeval)
{
long fsize;
int ftime;
int fsec;
char *p;

ftime=atoi(timeval);
p=strchr(timeval,':');
if(p)
{
   p++;
   fsec=atoi(p);
   ftime=(ftime*60)+fsec;
}
fsize= ftime*TIMESIZE;
DisplayCommaDelim(fsize);
printf("%6s ",timeval);
printf("\n");
}

void displayTime(long fs,char *fl)
{
int ftime;
int fmin;
int fsec;
ftime=fs/TIMESIZE;
fmin=ftime/60;
fsec=ftime%60;
DisplayCommaDelim(fs);
printf(" %d:%02d %s \n",fmin,fsec,fl) ;
}

int main(int argc,char *argv[])
{
int i;
long fsize;
int parm=1;
char fname[256];
FILE *st;
while(parm<argc)
{
  if(argv[parm][0]==FILESIZE)
  {
    fname[0]=0;
    fsize=atol(&argv[parm][1]);
  }
  else
  if(argv[parm][0]==FILETIME)
  {
     fsize=-1;
  }
  else
  {
     strcpy(fname,argv[parm]);
     st=fopen(fname,"rb");
     if(st==NULL)
     {
	printf("file %s was not opnened\n",fname);
	fsize=-1;
     }
     else
     {
	fseek(st,0,SEEK_END);
	fsize=ftell(st);
	fclose(st);
     }
   }
   if(fsize>=0)
	displayTime(fsize,fname);
   else displaySize(&argv[parm][1]);
   parm++;
  }
  getch();
  return 0;
}

i want to ask bout fopen in the code prog.also i have read a review bout standard I/O include fopen.then i compare using fopen in that review n in the prog.

prog:

FILE *st;
strcpy(fname,argv[parm]);
st=fopen(fname,"rb");
if(st==NULL)
-----


review:

FILE *fptr;
fptr = fopen("a:\\data\\customer.dat","w+");
if (fptr == NULL)
------

1. what does the prog do with line :strcpy(fname,argv[parm]).since it is no path file can the prog open the file ?

2.i also read that if use "rb",the file must exist.so if the code doesn't use the path file ,i think the file doeesn't exist and the prog can't run.is it true?

2.is it any difference with the review & the prog,and if the both code can used which code is better?

i am still confuse bout code in c++,so i'll thank for ur help .
  #2  
Old 23-Jul-2005, 07:16
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 117
L7Sqr has a spectacular aura about
Quote:
1. what does the prog do with line :strcpy(fname,argv[parm]).since it is no path file can the prog open the file ?

strcpy copies one string to another. So if argv[parm] contained "welcome" then strcpy (fname, argv[parm]) would set fname to "wlecome". (Although, I would strongly suggest using strncpy or strndup as they limit the number of characters you can copy)

Quote:
2.i also read that if use "rb",the file must exist.so if the code doesn't use the path file ,i think the file doeesn't exist and the prog can't run.is it true?

Using "r", which is a request to read, requires that there is a file there to actually read from.

Quote:
2.is it any difference with the review & the prog,and if the both code can used which code is better?

The only difference in the two is that in one you create a variable containing the text for fopen, and in the other you pass the text directly to fopen. Assuming that fname == "a:\\data\\customer.dat" there is no difference.
Note, your program opens the file for "rb", the sample opens for "w+", THAT is different, but you can google for fopen to find more about it's flags
  #3  
Old 24-Jul-2005, 20:05
likeit likeit is offline
New Member
 
Join Date: Jul 2005
Posts: 11
likeit is on a distinguished road

reply


CPP / C++ / C Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

#define TIMESIZE 176555L
#define FILESIZE '#'
#define FILETIME '&'

void DisplayCommaDelim(long fsize)
{
long parts[6];
int p;

p=0;
while(fsize>0)
{
parts[p]=fsize % 1000;
fsize/=1000;
p++;
}

printf("%3d",parts[--p]);
while(p>0)
{
printf(",%03d",parts[--p]);
}
}

void displaySize(char *timeval)
{
long fsize;
int ftime;
int fsec;
char *p;

ftime=atoi(timeval);
p=strchr(timeval,':');
if(p)
{
   p++;
   fsec=atoi(p);
   ftime=(ftime*60)+fsec;
}
fsize= ftime*TIMESIZE;
DisplayCommaDelim(fsize);
printf("%6s ",timeval);
printf("\n");
}

void displayTime(long fs,char *fl)
{
int ftime;
int fmin;
int fsec;
ftime=fs/TIMESIZE;
fmin=ftime/60;
fsec=ftime%60;
DisplayCommaDelim(fs);
printf(" %d:%02d %s \n",fmin,fsec,fl) ;
}

int main(int argc,char *argv[])                      //step 1
{
int i;
long fsize;
int parm=1;                                              //step 2
char fname[256];
FILE *st;
while(parm<argc)                                     //step 3
{
  if(argv[parm][0]==FILESIZE)
  {
    fname[0]=0;
    fsize=atol(&argv[parm][1]);
  }
  else
  if(argv[parm][0]==FILETIME)
  {
     fsize=-1;
  }
  else
  {
     strcpy(fname,argv[parm]);
     st=fopen(fname,"rb");
     if(st==NULL)
     {
  printf("file %s was not opnened\n",fname);
  fsize=-1;
     }
     else
     {
  fseek(st,0,SEEK_END);
  fsize=ftell(st);
  fclose(st);
     }
   }
   if(fsize>=0)
  displayTime(fsize,fname);
   else displaySize(&argv[parm][1]);
   parm++;
  }
  getch();                                             //step 4
  return 0;                                           //step 5
}

i use trace info(f7) to know the steps in the prog.there are only 5 step as seen above and after i run the prog ,there is no output .

why does the steps only 5 and i need help to make code so the prog have output
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
fopen fails to create a file pointer gaoanyu C++ Forum 3 05-Jun-2005 11:16
Definitions for Standard Library Functions? BobbyMurcerFan C++ Forum 8 07-Jan-2005 10:45
[Discussion][Tutorial] Standard I/O Pandiani C Programming Language 1 09-May-2004 02:30
[Tutorial] Standard I/O aaroncohn C Programming Language 20 27-Feb-2004 21:07
New wi-fi standard approved - here comes 24-54Mbps jrobbio Open Discussion Forum 1 17-Jul-2003 00:29

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:57.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.