Overclock.net banner
1 - 10 of 10 Posts

·
Registered
Joined
·
579 Posts
Discussion Starter · #1 ·
i want to check if a file is not empty but i have no idea how GetLenght works.
msdn link doesn't help so could someone please explain it to me? im thinking i need to #inlude something because im getting error C3861: 'GetLenght': identifier not found.
 

·
Registered
Joined
·
61 Posts
I have a feeling GetLength() or (string.Length??) is part of the String class.

Meaning that you will probably have to read the contents of the file into a string variable and then call VarName.GetLength() (Off the top of my head from last year but give it a go!)
 

·
Registered
Joined
·
579 Posts
Discussion Starter · #5 ·
Code:
Code:
cout << "enter the name of the file to change n(if u enter the wrong one close the window and try again.)" << endl;
cin >> file;
result = GetLenght(file);
the code gets a file name , opens the file , rewrites the text into another file while editing it but if i input a file that doesn't exist it starts an endless loop of
Code:
Code:
newfile << "/n"
and i don't want somebody by accident typing the wrong name and without noticing getting a huge txt file. i've tried opening the file with
Code:
Code:
ios::out
but it didn't help.

edit :

Quote:


Originally Posted by SteveJMason
View Post

I have a feeling GetLength() or (string.Length??) is part of the String class.

Meaning that you will probably have to read the contents of the file into a string variable and then call VarName.GetLength() (Off the top of my head from last year but give it a go!)

yes! thanks for the help.its actually VarName.lenght(). now it creates a file like asf.sd but its still better then the unopenable file i was getting earlier.thanks for the help everybody!
 

·
Registered
Joined
·
579 Posts
Discussion Starter · #7 ·
got another problem

Code:
Code:
cout << "create a new file with the updated script or change it in the original file?n 1 = create new file 2 = change original " << endl;
cin >> create;
if(create != 1 || create !=2){
create = 1;
cout << "using defaul (1)." << endl;
}
cout << "enter the name of the file to change." << endl;
cin >> file;
ifstream yet;
yet.open(file.c_str());
yet.getline (buffer,100000);
tempstring.assign(buffer);
if(tempstring.length() == 0){
cout << "the file doesn't exist.closing." << endl;
system("PAUSE");
return 0;
}
if the first if statement is true then it ignores
Code:
Code:
cin >> file
and since theres no file the
Code:
Code:
if(tempstring.length() == 0){
is also true and the app closes. any way to go around this?
 

·
Banned
Joined
·
2,461 Posts
Wrong function!

http://msdn.microsoft.com/en-us/libr...55(VS.85).aspx

GetFileSize Function

Code:

Code:
DWORD WINAPI GetFileSize(
  __in       HANDLE hFile,
  __out_opt  LPDWORD lpFileSizeHigh
);
Quote:
Parameters

hFile [in]
A handle to the file.

lpFileSizeHigh [out, optional]
A pointer to the variable where the high-order doubleword of the file size is returned. This parameter can be NULL if the application does not require the high-order doubleword.

Return Value

If the function succeeds, the return value is the low-order doubleword of the file size, and, if lpFileSizeHigh is non-NULL, the function puts the high-order doubleword of the file size into the variable pointed to by that parameter.

If the function fails and lpFileSizeHigh is NULL, the return value is INVALID_FILE_SIZE. To get extended error information, call GetLastError. When lpFileSizeHigh is NULL, the results returned for large files are ambiguous, and you will not be able to determine the actual size of the file. It is recommended that you use GetFileSizeEx instead.

If the function fails and lpFileSizeHigh is non-NULL, the return value is INVALID_FILE_SIZE and GetLastError will return a value other than NO_ERROR.

Code:

Code:
HANDLE file = CreateFile("myfile.txt", GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (file == INVALID_HANDLE_VALUE)
{
    // File doesn't exist or some other issue
    // Error codes are listed here http://msdn.microsoft.com/en-us/library/ms681381(VS.85).aspx
    printf("Error: %u", GetLastError());
    ExitProcess(0);
}
DWORD filesize = GetFileSize(file, 0);
CloseHandle(file);
 

·
Banned
Joined
·
2,461 Posts
Another method which may execute a bit faster if you don't plan on ever opening up the file is:

Code:
Code:
WIN32_FILE_ATTRIBUTE_DATA data;

BOOL success = GetFileAttributesEx("myfile.txt", GetFileExInfoStandard, &data);
if (success)
{
    DWORD filesize = data.nFileSizeLow;
}
else
{
    //file doesnt exist or something
    printf("Error: %u", GetLastError());
}
 
1 - 10 of 10 Posts
Top