|
Is there a basic windows dialog we can use when making an application that has a "Browse...", "Open...", or "Save..." option, or do I have to create my own? How would I use it, if there is one provided for me already?
Danny
|
|
|
|
|
|
How about GetOpenFileName() or SHBrowseForFolder() ?
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
|
Hello,
how can I read a file line by line and get the first character of each line ? I have a config file where comments are specified with a ; as the first character.
Please help
With kind regards,
RedDragon2kx
|
|
|
|
|
I like to use fstream. For example,
#include < fstream.h ><br />
fstream SomeFile;<br />
SomeFile.open(pathToOpen, ios::in | ios::nocreate | ios::noreplace);<br />
if(!SomeFile){<br />
output = "Error!";<br />
}<br />
SomeFile.getline(TextBuffer, 5000, '\n');
In the example, SomeFile is an fstream object. Open the file with open() (Check MSDN for full stats and flags with this). pathToOpen is a string with the path of the file you want to open. If the string is just a file name it will look in the current path. The ios statements are different flags. This one opens as input (you are reading from the file) and not overwriting or creating the file. An error statement. Then, for your question, the getline(). TextBuffer is a character array the line will be stored at, 5000 is the number of characters to be read, and the last option says to stop at the newline tag if one is read before 5000 characters.
It's easy from there to look at TextBuffer[0] (the first character of the line) or to scan the TextBuffer array until you reach the first word (and make sure it's not prefaced with a ';'. Can comments begin after a line? You can write a loop to look through the TextBuffer until the ';' is reached and replace it with a '\0' (null terminating character). I wouldn't suggest replacing the '\n' in the code with a ';' because, while that might work sometimes, does every line have to have a ';' character? It may then read in several lines.
Hope this helps and is descriptive enough!
Danny
|
|
|
|
|
Hi,
Thank you for your quick answer.
<br />
ifstream fin(strFilename);<br />
char buffer[2000];<br />
while(!fin.eof())<br />
{<br />
fin.getline(buffer,2000,'\n');<br />
str += buffer;<br />
}<br />
This is the code i have now. But it seems the while loop is kicked into an endless loop. Do you know why?
With best regards,
Benedikt
|
|
|
|
|
If you have your program display the text it reads in every loop iteration, it might be easier to see what is going on.
|
|
|
|
|
RedDragon2k wrote:
ifstream fin(strFilename);
This part should work, but I'm not as familiar with using ifstream as just plain fstream. As an alternative, you could try
<br />
fstream fin;<br />
fin.open(strFilename, ios::in);<br />
But what you have should work. I have two other thoughts:
Is the file referred to by strFilename in the same folder as your program? It's possible that the call ifstream fin(strFilename); is simply not opening the file. If strFilename is a string with the whole path, is the file really there? If strFilename is a string with just the file, is the file in the same directory as the program? Don't forget your error checking!
Second idea, as mentioned in a response earlier, have you tried outputting the buffer at each iteration. If so, is anything being outputted? If not, then the file was probably never opened and fin does not point to a file descripter. Is the contents outputted appropriate? What are the contents of the file? If my previous guesses are incorrect, I guess I'd have to know what the file is.
Hope this helps!
Danny
|
|
|
|
|
|
Antoher way to do it, "the old fashion way".
bool ReadKonfig()
{
char lineBuf[300];
int i;
FILE * fil;
if((fil = fopen("C:\\TheFile.ini", "rt")) == NULL) {
// Couldn't open the file
return false;
}
// The file is open, read it.
while(fgets(lineBuf, sizeof(lineBuf), fil) != NULL) {
// We have a line
// Remove trailing crlf's.
for(i = strlen(lineBuf) - 1; i >= 0; i--) {
if(lineBuf[i] == '\r' || lineBuf[i] == '\n') lineBuf[i] = '\0';
}
// Ignore comments.
// The line is considdered a comment if the line starts with a ';', '#', space, tab or is empty.
if(lineBuf[0] == ';' || lineBuf[0] == '#' || lineBuf[0] == ' ' || lineBuf[0] == '\t'
|| !strlen(lineBuf)) continue;
// It's a real line, process it.
// ...
}
fclose(fil);
return true;
}
|
|
|
|
|
Thank you, but it still seems that the while loop gets endless for some reason, not sure why.
With best regards,
RedDragon2kx
|
|
|
|
|
Hello.
The while loop reads line after line from the text file. When the last line has been read, fgets returns NULL and the while-loop breaks.
The original code (from which I copied the code) works just fine.
Are you sure your infile is a pure text-file? If you read it in Notepad, does it look OK?
Try the code with another text file. Create one i Notepad and try it out.
The code should work OK, assuming it's a normal ANSI-textfile. The code doesn't work for Unicode files, though. Do you think that could be the problem?
|
|
|
|
|
Hello,
I could fix the problem. It was a problem with the CString object because I used += and the String was getting to large or something like this. I'm now using just = and everything works fine
With best regards,
Benedikt
|
|
|
|
|
Great!
Good luck with the rest of your program.
Kakan
|
|
|
|
|
I have created a MFC program. I have selected that Visual Studio should prioritize size when I build it in release. This makes the size of the exe-file 18kb. BUT it still uses 4Mb of RAM when it's run. I don't use any big resources/data types/variables. Is there any way to minimize the amount of RAM the program uses or is it MFCs fault?
|
|
|
|
|
Where did you come up with it uses 4 MB RAM?
I bet it might use 4 MB of 'virtual memory' but what is the program's working set size? That is how much 'real' RAM it might be using at any time. The rest of it is sitting off in the paging file, so who would care?
|
|
|
|
|
Well, I used the most obvious way - Ctrl+Alt+Del (taskmanager).
|
|
|
|
|
See here.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
Oh, I see! Thanks, now I'm not worried anymore!
|
|
|
|
|
Any quick function or class to get the IP address of the PC the program runs on ?
Thanks,
Shay
|
|
|
|
|
One way is to call gethostname() followed by gethostbyname() .
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
|
Does this code look reasonable ?
I did get my PC name on the netwrok but not actual IP address
<br />
char * hostname=new char[50];<br />
gethostname (hostname,50);<br />
<br />
hostent *host_info=new hostent;<br />
host_info=gethostbyname(hostname);<br />
char FAR** iplist=host_info->h_addr_list;<br />
char *ip=iplist[0];
|
|
|
|
|
char hostname[51] = { 0 };
gethostname(hostname, 50); will save you one delete[].
hostent* host_info = gethostbyname(hostname) will save you one memory leak.
char* ip = host_info->h_addr will save you some typing.
A better code snippet:
char hostname[51] = { 0 };
gethostname(hostname, 50);
hostent* host_info = gethostbyname(hostname);
char* ip = host_info->h_addr;
Good music: In my rosary[^]
|
|
|
|