LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-03-2005, 05:22 PM   #1
purefan
Member
 
Registered: Aug 2003
Location: Sweden
Distribution: Ubuntu 10.04
Posts: 99

Rep: Reputation: Disabled
files size to char[]


hello!
well I am under XP using VC++6.
So, I need to get the file size of a bunch of files into char[].
Code:
//Declaration of token:
char *token[512] = {0};
later token holds all the file names I need.
The way for getting a file's size I use is:

Code:
WIN32_FILE_ATTRIBUTE_DATA  wfad;
GetFileAttributesEx(TEXT("File.txt"), GetFileExInfoStandard, &wfad);
printf("Size of x.bat is %u\n", wfad.nFileSizeHigh);
but using a for loop doesnt work, it shows the very same value for every token[i].
so if I use:
Code:
//say the number of files is 16
for (int J=0;J<=15;J++)
{
WIN32_FILE_ATTRIBUTE_DATA  wfad;
GetFileAttributesEx(TEXT(token[J]), GetFileExInfoStandard, &wfad);
printf("Size-> %u\n", wfad.nFileSizeHigh);
}//for
it will show the same number for all the token[J].
how can I store the file sizes in token[] to a char[]??? (lets name it FileSizes[])

also if anyone knows what is nFileSizeHigh and nFileSizeLow that would hel me understand better what's going on.

thanks a lot! =D
 
Old 04-03-2005, 06:38 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Lookiing back at some code that I wrote over 5 years ago, I found this:

Code:
            clsFileInfo.setFileSize
               ( 
                  (stWinFileData.nFileSizeHigh * MAXDWORD) + stWinFileData.nFileSizeLow
               );
In your examples, I'm guessing that the files are small and nFileSizeHigh is 0.
 
Old 04-03-2005, 07:14 PM   #3
purefan
Member
 
Registered: Aug 2003
Location: Sweden
Distribution: Ubuntu 10.04
Posts: 99

Original Poster
Rep: Reputation: Disabled
hmmmm....im sorry but I dont understand how to implement this...
my files are variable, so it is not always the same value...
 
Old 04-03-2005, 07:20 PM   #4
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
in the printf that you posted replace wfad.nFileSizeHigh with:
Code:
( wfad.nFileSizeHigh * MAXDWORD ) + wfad.nFileSizeLow
 
Old 04-03-2005, 08:04 PM   #5
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
CAVEAT This code is untested.
Have you tried:
Code:
int n_read += n = read ( int fd, char *buf, int n )
              printf("The size of the file is:  %d\n", n_read  );
 
Old 04-03-2005, 08:22 PM   #6
purefan
Member
 
Registered: Aug 2003
Location: Sweden
Distribution: Ubuntu 10.04
Posts: 99

Original Poster
Rep: Reputation: Disabled
Crabboy: It worked for a manual insertion of the vector:
Code:
	GetFileAttributesEx(TEXT(token[1]), GetFileExInfoStandard, &wfad);
printf("Size of x.bat is %u\n", ( wfad.nFileSizeHigh * MAXDWORD ) + wfad.nFileSizeLow) ;
but not for a for loop:
Code:
	for (int aj=0;aj<=10;aj++)	
	{
	GetFileAttributesEx(TEXT(token[aj]), GetFileExInfoStandard, &wfad);
printf("Size -->%u\n", ( wfad.nFileSizeHigh * MAXDWORD ) + wfad.nFileSizeLow) ;
	}//for
Dave Kelly: it did not compile right away, I set it for
Code:
int n_read;
int n;
n_read += n = read ( int fd, char *buf, int n );
printf("The size of the file is:  %d\n", n_read  );
but read is undeclared, do I need to include a library??

[/code]

Last edited by purefan; 04-03-2005 at 08:23 PM.
 
Old 04-03-2005, 09:34 PM   #7
purefan
Member
 
Registered: Aug 2003
Location: Sweden
Distribution: Ubuntu 10.04
Posts: 99

Original Poster
Rep: Reputation: Disabled
I found a function that works!!!!!!!!

Code:
// obtaining file size
#include <iostream.h>
#include <fstream.h>

const char * filename = "example.txt";

int main () {
 long l,m;
 ifstream file (filename, ios::in|ios::binary);
 l = file.tellg();
 file.seekg (0, ios::end);
 m = file.tellg();
 file.close();
 cout << "size of " << filename;
 cout << " is " << (m-l) << " bytes.\n";
 return 0;
}
it works very when assigning to an unsigned long[]!!!
so it is all solved now!!
I didnt use a handle or a complicated structure so it is great!!
Thanks all for the help!!
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
reading a char string of variable size in C introuble Programming 3 05-08-2005 01:07 PM
C Problem---convert char to char* totti10 Programming 11 11-06-2004 11:32 AM
char array of size 10 can read morethan 10 chars!!!!! pippet Programming 13 07-12-2004 01:44 AM
invalid conversion from `char' to `const char* bru Programming 6 05-09-2004 03:07 PM
increase point size size when printing PDF files newlin Red Hat 0 09-27-2003 07:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration