473,765 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I check if a file is empty in C??

Hi group..I am writing a playlist management protocol where I have a
file that holds all the playlists and a file that holds all the
songs....before a playlist is created I need to check to see if the
playlist file is empty so that I can assign an integer value to a
playlist id field if it is the first playlist being written to the
file....can anyone help?? Thanks in advance

wetherbean

Nov 14 '05
19 32329
Chris Torek wrote:
In article <11************ *********@o13g2 000cwo.googlegr oups.com>
wetherbean <bj******@gmail .com> wrote:
Hi group..I am writing a playlist management protocol where I have a
file that holds all the playlists and a file that holds all the
songs....befo re a playlist is created I need to check to see if the
playlist file is empty so that I can assign an integer value to a
playlist id field if it is the first playlist being written to the
file....can anyone help?? Thanks in advance

Definition: a file is "empty" if you cannot read anything from it.
(Is there a potential flaw in this definition? [I say yes, but
this is an exercise; you are supposed to identify potential flaws,
not just answer "yes" or "no". :-) ])

Conclusion: open the file and try to read from it. If you get
some data, it is not empty. If you get EOF immediately, it is
empty.


Leave it to Chris to cut through to the chase and give the KISS answer.

Anyway, this is how I used to do it many moons ago, but now that we have
long longs and files > 2 gigs are common place I will have to rewrite my
code.

/*************** *************** *************** *************** **********/
/* File Id. filelen.c. */
/* Author: Stan Milam. */
/* Date Written: 5 May 92. */
/* Description: */
/* Implement a function to determine the size of a file in bytes. */
/* Useful for determining if a file is empty too. */
/* */
/* (c) Copyright 2005 by Stan Milam. */
/* All rights reserved. */
/* */
/*************** *************** *************** *************** **********/

#include <stdio.h>
#include <sys/types.h>
#ifdef _UNIX
# include <unistd.h>
#else
# if defined (__TURBOC__) || defined (__POWERC)
# include <io.h>
# endif
#endif

/* $Revision$ */
extern char tb_copyright[];

/*************** *************** *************** *************** **********/
/* Name: */
/* filelength(). */
/* */
/* Synopsis: */
/* #include "toolbox.h" */
/* long filelength( in fd ); */
/* */
/* Description: */
/* This function will return the length of a file in bytes. */
/* */
/* Arguments: */
/* int fd - A file descriptor. */
/* */
/* Returns Value: */
/* A value of type long representing the size of the file. -1L is */
/* returned when an error occurs. */
/* */
/*************** *************** *************** *************** **********/

long
filelength (int fd)
{
long rv, here;

if (( here = lseek(fd, 0L, SEEK_CUR)) == -1L )
rv = -1L;
else {
rv = lseek(fd, 0L, SEEK_END);
lseek(fd, here, SEEK_SET);
}
return rv;
}
Nov 14 '05 #11
>>Anyhow, using stat(), or a stat like function, should be a more
efficient than having to open each file and then attempting to read from
it to see if it contains any data.

Have you tested this? In particular, have you compared the
performance of the sequence:

stat the file to find out if it is empty.
then, open the file and if it was empty earlier, write an ID, but if it
was not empty, read the ID;
then, write any data;
finally, close the file.

vs:

open the file;
try to read the ID; if none available, write an ID;
then, write any data;
finally, close the file.


Hrm....I reread the original post by wetherbean. It looks like, for what
s/he is trying to do, you're right using the fopen approach is the
better way to go.

The post I replied to just mentions "How do I check if a file is empty
in C?" and a solution involving opening the file, fseeking to the end of
the file and then looking at the value of ftell to see if it's empty or
not.

Going off of just that, I thought, gee it seems like checking if a file
is empty or not by using fopen(), fseek(), ftell(), and then presumably
fclose() is much more complicated than just a single call to stat().
Also, the fopen(), fseek()..... sequence might involve the C library
reading part of the file into a buffer in preparation for calls that
normally come after commands like fopen, e.g. fgets, fgetc.

In contrast, the stat() approach only requires one command, and will
probably only involve the system retrieving the meta data associated
with the file(s). This meta data would also presumably be read in by the
underlying OS whenever fopen is called as well. Also, stat(), or
something very similar to it, for example, _stat() on Windows, is
ubiquitous enough that there's a >95% chance** that it's available on
whatever system the original poster is using.

So, if someone is just interested in whether a file is empty or not, for
example, if they just want to build a list of either empty or non-empty
files, using stat()/_stat() seems like a fairly reasonable approach. Or,
at least, something a programmer should be aware of when deciding what
approach to take.

Anyhow, like I said above, given what wetherbean is trying to do (i.e.
the file is going to be written to after the empty/non-empty check),
then, yeah, using the approach you suggested, something like fopen() +
fgetc() with a test for an immediate EOF, seems like the way to go.

-Dan

** Okay, this value was carefully obtained by the rule that "70% of
statistics are made up on the spot". But, it's still not entirely baseless.
Nov 14 '05 #12
In article <42************ **@gmail.com>
Daniel Cer <Da********@gma il.com> wrote:
The post I replied to just mentions "How do I check if a file is empty
in C?" and a solution involving opening the file, fseeking to the end of
the file and then looking at the value of ftell to see if it's empty or
not.
(Which is not a 100% reliable method, due to the restrictions on
seek positions -- ANSI/ISO C says nothing at all about text files,
and says that binary files may contain arbitrary padding. Of course
it works on most real systems, especially for simple empty/nonempty
tests.)
In contrast, the stat() approach only requires one [call], and will
probably only involve the system retrieving the meta data associated
with the file(s).
Yes; although as it happens, opening a file is usually just about
the same amount of work. (The "extra" work in opening the file,
if any, lies in allocating data structures to track the open file.
But, just to take an example of a system I work on now: in vxWorks,
stat() is implemented internally as open() followed by fstat()
followed by close() -- so there is no savings at all! Indeed,
virtually all operations in vxWorks require opening the file first,
which may be a problem for POSIX semantics. A POSIX system allows
renaming or removing a file that you have no permission to open,
for instance.)
This meta data would also presumably be read in by the
underlying OS whenever fopen is called as well. Also, stat(), or
something very similar to it, for example, _stat() on Windows, is
ubiquitous enough that there's a >95% chance** that it's available on
whatever system the original poster is using.
It is pretty darn common -- but note that issues like "sometimes
it is named _stat" are already an annoyance.
So, if someone is just interested in whether a file is empty or not, for
example, if they just want to build a list of either empty or non-empty
files, using stat()/_stat() seems like a fairly reasonable approach.


To build a list of files, you usually have to do something else
that cannot be done in ANSI/ISO C: read directories. There is no
harm going beyond the Standard in this case. If you have to do
something that is impossible in Standard C, you might as well use
non-Standard C. :-)

The trickier tradeoffs come in when you *can* do it -- whatever
"it" is -- in Standard C, but you can do it "better" with some
non-standard calls. Now you face the same problem as the fellow
who wants to buy a car, and can get a clapped-out Yugo for a hundred
bucks, or a nice shiny slightly-used Lexus or BMW for a mere $25
grand: "good, fast, cheap -- pick any two." :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #13
Hello...thanks for all your input guys....this was the biggest response
I have had yet. I have found for my purpose the fseek.ftell method
seems to be the best...although I did encounter a small
problem....afte r I check if the file is empty I have to call rewind()
so fread in a loop from the beginning of the file to get the playlist
id of the last file entry before I can write the new playlist entry to
the file.....this part of the code is successful but within my
while(!feof) the code is doing an extra run as if the EOF has gotten
pushed up a line....can anyone explain this?? Here is the portion of
my code where I was trying to test if the fwrites were actually
producing the correct info....if anyone needs more explanation let me
know...Thanks in advance

weatherbean
fseek(list_file ,0,SEEK_END);

if(ftell(list_f ile)==0){
//printf("ftell=0 \n");
playlist.list_i d=1;

fwrite(&playlis t,sizeof(playli st),1,list_file );
}

else if(ftell(list_f ile)!=0){
rewind(list_fil e);
while(!feof(lis t_file)){

fread(&playlist ,sizeof(playlis t),1,list_file) ;

printf("%s\n",p laylist.list_na me);
printf("%d\n",p laylist.list_id );

prev_id=playlis t.list_id;

}//end while

playlist.list_i d=prev_id+1;

strcpy(playlist .list_name,list name);

fwrite(&playlis t,sizeof(playli st),1,list_file );

Nov 14 '05 #14
"wetherbean " <bj******@gmail .com> writes:
Hello...thanks for all your input guys....this was the biggest response
I have had yet. I have found for my purpose the fseek.ftell method
seems to be the best...although I did encounter a small
problem....afte r I check if the file is empty I have to call rewind()
so fread in a loop from the beginning of the file to get the playlist
id of the last file entry before I can write the new playlist entry to
the file.....this part of the code is successful but within my
while(!feof) the code is doing an extra run as if the EOF has gotten
pushed up a line....can anyone explain this?? Here is the portion of
my code where I was trying to test if the fwrites were actually
producing the correct info....if anyone needs more explanation let me
know...Thanks in advance


The feof() fucntion tells you that the last read operation hit the end
of the file, not that the next one is going to. (It also doesn't tell
you when an error occurs, so your loop could be an infinite one.)

fread() returns the number of bytes it actually read; use that to
determine when you've reached the end of the file. (You can then use
the feof() function to distinguish between reaching the end of the
file and encountering an error.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #15
Thanks...that worked great....I also encountered a problem putting with
putting my switch into a loop.....I want all of the user errors to
return to the second menu I have but when I try to put the second
switch in my program in a loop it segfaults...I think it might be
easier to show you what I mean if I just post the entire code here...I
know some consider it bad practice to put switch statements in loops
but at this point I am just trying to make it work....lol..an y
suggestions?? Thanks in advance

Wetherbean
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct song_rec{
char artist[30];
char album[35];
int track;
int song_id;};

struct playlist_rec{
char list_name[20];
int list_id;};

struct user_rec{
char name[15];
int pw;};
int main(int argc,char**argv ) {

FILE* user_file;
FILE* list_file;
FILE* song_file;
struct user_rec user;
struct playlist_rec playlist;
struct song_rec song;
int choice=0,found= 0,password=0,re gistered=0,offs et=0;
int first=0,choice2 =0,prev_id=0,co unt=0,list_id;
char uname[15];
char listname[20];
char song_file_name[25];
char list_file_name[25];

printf("1)REGIS TER\n");
printf("2)LOGIN \n");
printf("->");
scanf("%d",&cho ice);

switch(choice){

case 1:
while(registere d!=1){
found=0;
printf("Enter desired login name: ");
scanf("%s",unam e);
printf("%s\n",u name);

user_file=fopen ("user_file.dat a","a+");

while(!feof(use r_file)&&found! =1){
fread(&user,siz eof(user),1,use r_file);

if (strcmp(uname,u ser.name)==0){
found=1;
printf("Login name already in use\n");
fclose(user_fil e);
}
}//end while

if (found==0){
registered=1;
printf("Enter desired password: ");
scanf("%d",&pas sword);
strcpy(user.nam e,uname);
user.pw=passwor d;
fwrite(&user,si zeof(user),1,us er_file);
printf("Registr ation Successful\n");
printf("Usernam e: %s\n Password:
%d\n",user.name ,user.pw);
fclose(user_fil e);}

}
break;

case 2:

user_file=fopen ("user_file.dat a","rb");

printf("Enter username: ");
scanf("%s",unam e);
printf("Enter password: ");
scanf("%d",&pas sword);
while(!feof(use r_file)&&strcmp (user.name,unam e)!=0){
fread(&user,siz eof(user),1,use r_file);
if (strcmp(user.na me,uname)==0 && password==user. pw){
printf("Logged In....\n\n");
sprintf(list_fi le_name,"%s.pls t",user.name );
sleep(1);}

else{
printf("User name and password do not
match\n");
exit(1);}
}

case 3:

/******ANSI CODE FOR CLEAR SCREEN*******/

printf(" \033[2J");

//------------------------------------ >
//I want the code after this to loop
// until choice2==7 but it segfaults when I add in the while loop

// while(1){

printf("Hello %s, Enter the number of your
choice\n");
printf("1)Creat e Playlist\n");
printf("2)Add song to a playlist\n");
printf("3)Delet e Song From a playlist\n");
printf("4)Rando mize playlist\n");
printf("5)Delet e Playlist\n");
printf("6)Displ ay Playlist File\n");
printf("7)Exit\ n");
printf("->");
scanf("%d",&cho ice2);

switch(choice2) {

/**************C REATE
PLAYLIST******* *************** ******/

case 1:
printf("Enter name of new playlist: ");
scanf("%s",list name);
//
sprintf(list_fi le_name,"%s.pls t",user.name );
list_file=fopen (list_file_name ,"a+");
/**************s topped working
here*********** ***/
fseek(list_file ,0,SEEK_END);

if(ftell(list_f ile)==0){
//printf("ftell=0 \n");
playlist.list_i d=1;
strcpy(playlist .list_name,list name);

fwrite(&playlis t,sizeof(playli st),1,list_file );
}

else if(ftell(list_f ile)!=0){
rewind(list_fil e);

while(fread(&pl aylist,sizeof(p laylist),1,list _file)!=0){

if
(strcmp(playlis t.list_name,lis tname)==0){
printf("Playlis t Name
already in use\n");
exit(0);}
printf("%s\n",p laylist.list_na me);
printf("%d\n",p laylist.list_id );

prev_id=playlis t.list_id;

}//end while

playlist.list_i d=prev_id+1;

strcpy(playlist .list_name,list name);

fwrite(&playlis t,sizeof(playli st),1,list_file );
}//else if

//printf("%d\n",p laylist.list_id );
fclose(list_fil e);

break;
/*************** *******ADD SONG TO
PLAYLIST******* *************** ***/
case 2:
printf("Enter name of playlist to add to: ");
scanf("%s",list name);
list_file=fopen (list_file_name ,"a+");
printf("Enter Song Artist: ");
scanf("%s",song .artist);
printf("Enter Album Title: ");
scanf("%s",song .album);
printf("Enter Track Number: ");
scanf("%d",&son g.track);

while(!feof(lis t_file)){

fread(&playlist ,sizeof(playlis t),1,list_file) ;

if(strcmp(playl ist.list_name,l istname)==0){
song.song_id=pl aylist.list_id;
}
}
fclose(list_fil e);
sprintf(song_fi le_name,"%s.sng ",uname);
song_file=fopen (song_file_name ,"a");
fwrite(&song,si zeof(song),1,so ng_file);

fclose(song_fil e);

break;
/*************** *************DI SPLAY CURRENT
PLAYLISTS****** *************** *************** */
case 6:
list_file=fopen (list_file_name ,"a+");

while(fread(&pl aylist,sizeof(p laylist),1,list _file)){

printf("%s\n",p laylist.list_na me);

}

case 7:
exit(0);
} //end switch 2
// } end while(1)

} //end switch 1

}

Nov 14 '05 #16
Thanks...that worked great....I also encountered a problem putting with
putting my switch into a loop.....I want all of the user errors to
return to the second menu I have but when I try to put the second
switch in my program in a loop it segfaults...I think it might be
easier to show you what I mean if I just post the entire code here...I
know some consider it bad practice to put switch statements in loops
but at this point I am just trying to make it work....lol..an y
suggestions?? Thanks in advance

Wetherbean
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct song_rec{
char artist[30];
char album[35];
int track;
int song_id;};

struct playlist_rec{
char list_name[20];
int list_id;};

struct user_rec{
char name[15];
int pw;};
int main(int argc,char**argv ) {

FILE* user_file;
FILE* list_file;
FILE* song_file;
struct user_rec user;
struct playlist_rec playlist;
struct song_rec song;
int choice=0,found= 0,password=0,re gistered=0,offs et=0;
int first=0,choice2 =0,prev_id=0,co unt=0,list_id;
char uname[15];
char listname[20];
char song_file_name[25];
char list_file_name[25];

printf("1)REGIS TER\n");
printf("2)LOGIN \n");
printf("->");
scanf("%d",&cho ice);

switch(choice){

case 1:
while(registere d!=1){
found=0;
printf("Enter desired login name: ");
scanf("%s",unam e);
printf("%s\n",u name);

user_file=fopen ("user_file.dat a","a+");

while(!feof(use r_file)&&found! =1){
fread(&user,siz eof(user),1,use r_file);

if (strcmp(uname,u ser.name)==0){
found=1;
printf("Login name already in use\n");
fclose(user_fil e);
}
}//end while

if (found==0){
registered=1;
printf("Enter desired password: ");
scanf("%d",&pas sword);
strcpy(user.nam e,uname);
user.pw=passwor d;
fwrite(&user,si zeof(user),1,us er_file);
printf("Registr ation Successful\n");
printf("Usernam e: %s\n Password:
%d\n",user.name ,user.pw);
fclose(user_fil e);}

}
break;

case 2:

user_file=fopen ("user_file.dat a","rb");

printf("Enter username: ");
scanf("%s",unam e);
printf("Enter password: ");
scanf("%d",&pas sword);
while(!feof(use r_file)&&strcmp (user.name,unam e)!=0){
fread(&user,siz eof(user),1,use r_file);
if (strcmp(user.na me,uname)==0 && password==user. pw){
printf("Logged In....\n\n");
sprintf(list_fi le_name,"%s.pls t",user.name );
sleep(1);}

else{
printf("User name and password do not
match\n");
exit(1);}
}

case 3:

/******ANSI CODE FOR CLEAR SCREEN*******/

printf(" \033[2J");

//------------------------------------ >
//I want the code after this to loop
// until choice2==7 but it segfaults when I add in the while loop

// while(1){

printf("Hello %s, Enter the number of your
choice\n");
printf("1)Creat e Playlist\n");
printf("2)Add song to a playlist\n");
printf("3)Delet e Song From a playlist\n");
printf("4)Rando mize playlist\n");
printf("5)Delet e Playlist\n");
printf("6)Displ ay Playlist File\n");
printf("7)Exit\ n");
printf("->");
scanf("%d",&cho ice2);

switch(choice2) {

/**************C REATE
PLAYLIST******* *************** ******/

case 1:
printf("Enter name of new playlist: ");
scanf("%s",list name);
//
sprintf(list_fi le_name,"%s.pls t",user.name );
list_file=fopen (list_file_name ,"a+");
/**************s topped working
here*********** ***/
fseek(list_file ,0,SEEK_END);

if(ftell(list_f ile)==0){
//printf("ftell=0 \n");
playlist.list_i d=1;
strcpy(playlist .list_name,list name);

fwrite(&playlis t,sizeof(playli st),1,list_file );
}

else if(ftell(list_f ile)!=0){
rewind(list_fil e);

while(fread(&pl aylist,sizeof(p laylist),1,list _file)!=0){

if
(strcmp(playlis t.list_name,lis tname)==0){
printf("Playlis t Name
already in use\n");
exit(0);}
printf("%s\n",p laylist.list_na me);
printf("%d\n",p laylist.list_id );

prev_id=playlis t.list_id;

}//end while

playlist.list_i d=prev_id+1;

strcpy(playlist .list_name,list name);

fwrite(&playlis t,sizeof(playli st),1,list_file );
}//else if

//printf("%d\n",p laylist.list_id );
fclose(list_fil e);

break;
/*************** *******ADD SONG TO
PLAYLIST******* *************** ***/
case 2:
printf("Enter name of playlist to add to: ");
scanf("%s",list name);
list_file=fopen (list_file_name ,"a+");
printf("Enter Song Artist: ");
scanf("%s",song .artist);
printf("Enter Album Title: ");
scanf("%s",song .album);
printf("Enter Track Number: ");
scanf("%d",&son g.track);

while(!feof(lis t_file)){

fread(&playlist ,sizeof(playlis t),1,list_file) ;

if(strcmp(playl ist.list_name,l istname)==0){
song.song_id=pl aylist.list_id;
}
}
fclose(list_fil e);
sprintf(song_fi le_name,"%s.sng ",uname);
song_file=fopen (song_file_name ,"a");
fwrite(&song,si zeof(song),1,so ng_file);

fclose(song_fil e);

break;
/*************** *************DI SPLAY CURRENT
PLAYLISTS****** *************** *************** */
case 6:
list_file=fopen (list_file_name ,"a+");

while(fread(&pl aylist,sizeof(p laylist),1,list _file)){

printf("%s\n",p laylist.list_na me);

}

case 7:
exit(0);
} //end switch 2
// } end while(1)

} //end switch 1

}

Nov 14 '05 #17
Jack Klein <ja*******@spam cop.net> wrote:
On Sun, 24 Apr 2005 00:35:26 GMT, Keith Thompson <ks***@mib.or g> wrote
in comp.lang.c:
Actually, the result of ftell() is meaningful if the file is opened
in binary mode.

C99 7.19.9.4p2:

The ftell function obtains the current value of the file position
indicator for the stream pointed to by stream. For a binary
stream, the value is the number of characters from the beginning
of the file. For a text stream, its file position indicator
contains unspecified information,
[snip]


The ftell() function is indeed meaningful for binary files, but using
fseek() first to get to the end, so ftell() can return the file size,
it not.


True, but the OP wanted to know whether the file is empty, not how large
it is if it's not. So ISTM that opening the file in text mode, fseek()
to the start of the file, save the indicator returned by ftell(),
fseek() to the end, compare the saved value to what ftell() returns now,
will work. If the two indicators are equal, the file is empty; if
they're not, you don't know _what_ the file size is, but you do know
that it's not zero.

Richard
Nov 14 '05 #18
On Tue, 26 Apr 2005 13:24:47 +0000, Richard Bos wrote:
Jack Klein <ja*******@spam cop.net> wrote:
On Sun, 24 Apr 2005 00:35:26 GMT, Keith Thompson <ks***@mib.or g> wrote
in comp.lang.c:
> Actually, the result of ftell() is meaningful if the file is opened
> in binary mode.
>
> C99 7.19.9.4p2:
>
> The ftell function obtains the current value of the file position
> indicator for the stream pointed to by stream. For a binary
> stream, the value is the number of characters from the beginning
> of the file. For a text stream, its file position indicator
> contains unspecified information,
> [snip]


The ftell() function is indeed meaningful for binary files, but using
fseek() first to get to the end, so ftell() can return the file size,
it not.


True, but the OP wanted to know whether the file is empty, not how large
it is if it's not. So ISTM that opening the file in text mode, fseek()
to the start of the file, save the indicator returned by ftell(),
fseek() to the end, compare the saved value to what ftell() returns now,
will work. If the two indicators are equal, the file is empty; if
they're not, you don't know _what_ the file size is, but you do know
that it's not zero.

AFAICT the Deathstation could return a random but distinct value from each
call to ftell() in a text stream and maintaain an internal lookup table
to find the true offset in fseek(). So the fact that two return values are
different doesn't mean they represent different offsets.

Lawrence
Nov 14 '05 #19
Chris Torek wrote:
[ snip ]
Conclusion: open the file and try to read from it. If you get
some data, it is not empty. If you get EOF immediately, it is
empty.


Caveat: On non-Unix systems, open the file in "rb" binary mode. It is
possible for a non-empty file to have 0x1a its first byte. In text mode
reading this byte can give you EOF immediately (not what you wanted to see).

--
Joe Wright mailto:jo****** **@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
41375
by: Paul Telco | last post by:
Hello, I'm a new user designing a simple database to retrieve pre-prepared docunents for printing. I have five tables, a form to design the documents, a form to customise and retrieve the documents, a query to pull the data together from the tables and a report which formats the document with the custom data ready to print. It works very well unless:
30
35891
by: S. van Beek | last post by:
Dear reader A record set can be empty because the condition in the query delivers no records. Is there a VBA code to check the status of a record set, record set empty
1
5588
by: Oleg Ogurok | last post by:
Hi all, I want to use RegularExpressionValidator to enforce non-empty integer format in a TextBox. However, the validator doesn't give the error when the textbox is empty. For example, if ValidationExpression is \d+ or even \d{1,}, the validator still allows empty field. Must I use an additional RequiredFieldValidator? -Oleg.
3
30204
by: puja | last post by:
hi all, In asp.net 2.0 there is a limit on file size upload of 4MB. I know this limit can be increased in web.config file. Is there any way to check the file size before upload ? if user is trying to upload file size more than 4MB then just display a message that file size is too big and can't be uploaded. thanks
7
53925
by: lphang | last post by:
I am reading a simple text file that will contains three set of string values as follow: "1234567", "ABC123456", "" I have my codes as follow trying to check for an empty string, with the input string as above, I can never get it to display "testid is empty", Any idea why the test failed?: FileData = open(filename, "r") Filecontents = FileData.readline().strip() FileData.close()
1
4277
by: jtertin | last post by:
I am currently using the following code to make sure a file is writable (i.e. is not in use) by using the CanWrite method of the FileStream object. Using the code below, the TextWriter is still trying to write to the file and is throwing an exception (File... in use by another process) whenever the output file (strFileName) is open. I am trying to prevent it from writing to it at all if this is the case... Any insight? Public...
2
9843
Manikgisl
by: Manikgisl | last post by:
HI. How to check File exists in Web Share C# try { WebRequest request = HttpWebRequest.Create("http://www.microsoft.com/NonExistantFile.aspx"); request.Method = "HEAD"; // Just get the document headers, not the data. request.Credentials = System.Net.CredentialCache.DefaultCredentials; // This may throw a WebException:
3
8269
Frinavale
by: Frinavale | last post by:
Hi there! I'm hoping that someone knows how to check the size of a file before it is uploaded to the server using JavaScript. I have seen suggested solutions using an ActiveX control to check the file size; however, I'm not happy with this solution because my application is designed to work in multiple browsers and ActiveX is limited to Internet Explorer. I cannot check the file size on the web server because IIS throws the following...
3
2966
by: akshalika | last post by:
I want regular expression which check for empty. pls help me.
10
21095
by: klharding | last post by:
I am reading the contents of a text file into variables. All works well if the text file does not exist, or it does exist and contains 4 rows of data. But I need to account for rows being empty or null. How can I check this? string filename = @"C:\Program Files\Picture Capture\Settings.txt"; if (File.Exists(filename)) { StreamReader reader = new StreamReader(filename); string strAllFile =...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5277
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.