-
'EOL' question
Hello everyone,
I'm having some sort of problem with the code bellow....
CODE:
==============================================
ifstream source;
char *DT_Country=0
char temp[20];
source.open("Header.txt,ios::in | ios::nocreate);
if (source)
{
source.getline(temp,20,'\n');
if (temp[0] == 'EOL') DT_Country = "United States";
else DT_Country = temp;
}
==============================================
The idea is to check if a specific Input file exists and if it exists, copy the content of the first line to an Array. If the first line is empty, put "United States" on the variable DT_Country, if the line is not empty, copy the content of that line to the variable.
The problem is that somehow i'm not being able to evaluate the 'EOL' condition.... i've alredy tryed with '\n' with 'EOL' and 'eol' and the program just crash on run time..... it doesn't eaven gives me an error... just crash...... :confused:
Please, help needed...
Thaks
-
i'd suggest just testing for the null terminator, '\0' or 0
[ code ] [ /code ]tags work nicer ;)
-
thanks for the help.... nevertheless, my program still crashes in the exact same way...... how can I evaluate properly if the line i'm getting from the file is empty or not ????
-
are you absolutely sure that's where the program is crashing?
-
well... it seams so, cause if I remove the evaluation condition for the 'eol' from my program.... it works fine.....
But the intersting part is that if I do:
source.getline(temp ,20,'\n'); if (temp=='\0') DT_Country = "United States";
The program runs without crashing (although it doesn't evaluate the condition properly). But if I change the code to:
source.getline(temp ,20,'\n'); if (temp!='\0') DT_Country = "United States";
The program crashes.... I'm really lost... I've alredy tryed everything I could think about it, but nothing works...... please help....
-
first of all, this is not how you declare an array... you want to use char*DT_Country=new char[20];
this already tests if the file is open... no need to do further checking in this case...
Code:
if (temp[0] == 'EOL')
this is wrong on many levels... first, EOL is not a character... a character is at maximum two characters... for example, '/n' would be a character, or 'a' would be a character, but not 'character'. the closest thing to EOL you'll find is '/0'. second of all, it sounds like you want to be searching for something like EOF, which is a flag, and won't really be something you can take out of the stream... you'll need to do something like if(source.eof())
Code:
DT_Country = "United States";
else DT_Country = temp;
if you're using C-style strings (which it looks like you are), this won't work... either use the string class, or if you want to stick with c-style strings, you could just go with strcpy()
-
major_small said all I was going to say, but let me elaborate a little more for you.
First off, saying char *DT_Country = 0; creates a pointer containing the address zero.
Saying char *DT_Country = new char[20]; creates a pointer and loads it with the address of some dynamically allocated heap memory. This memory will have to be freed when you're done using it (using delete []).
Saying char DT_Country[20]; is a good choice in this case. It creates an array in static memory and its address is loaded into DT_Country.
temp is an array of characters ending with a null byte. Using the subscript operator (eg. temp[0]) accesses a single character at the given index. You can compare this character to another single character (enclosed in single quotes [eg. if (temp[0] == 'E')]; double quotes are used to surround null-terminated arrays of characters). You can not compare temp to another char array using something like if (temp == "EOL") because what that will do is compare the address contained in the pointer called temp to the address of the static array containing "EOL". To compare two strings, use strcmp(). If the two strings are identical it will return zero.
You cannot copy an array of characters from one array to another using something like DT_Country = "United States";. What this does is load the array pointer DT_Country with the address of the string "United States". (given you _can_ declare it as char *DT_Country; then just assign it to a static string using = you should try to avoid it while you are learning how to use C/C++). Use strcpy(destination, source) to copy a string to another. It will traverse the source string and copy each character into destination up to and including when it finds a null byte (because of this fact it is not a secure function to use, but you'll be fine with it for now).
-
Thanks 'LuckY' and 'major_small' your tips have solved my problem.
I was way out of the right path.... it seams that I've to buy some new books and trow my 10 year old college notes away.
This is how the code looks like now:
PHP Code:
ifstream source;
char *DT_Country = new char[20];
char *DT_Operator = new char[20];
source.open("Header.txt",ios::in | ios::nocreate);
if (source)
{
source.getline(DT_Country, 20, '\n'); if (DT_Country[0]=='\0') strcpy(DT_Country, "United States");
source.getline(DT_Operator,20,'\n'); if (DT_Operator[0]=='\0') strcpy(DT_Operator, "T-Mobile")
}
-
much better, dh. two things. First, I always prefer (and you may to) to use if (source.is_open()) instead of simply if (source). Secondly, just remember for every "new []" you must have a corresponding "delete []" (eg. delete [] DT_Country).