Thread: 'EOL' question

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    47

    Unhappy '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......

    Please, help needed...

    Thaks
    I'm a person with a simple taste...
    I only like the best.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    i'd suggest just testing for the null terminator, '\0' or 0

    [ code ] [ /code ]tags work nicer

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    47
    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 ????
    I'm a person with a simple taste...
    I only like the best.

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    are you absolutely sure that's where the program is crashing?

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    47
    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....
    I'm a person with a simple taste...
    I only like the best.

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Code:
    char *DT_Country=0
    first of all, this is not how you declare an array... you want to use char*DT_Country=new char[20];
    Code:
    if (source)
    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()
    Last edited by major_small; 07-16-2004 at 09:36 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    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).

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    47
    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_Country20'\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")
         } 
    I'm a person with a simple taste...
    I only like the best.

  9. #9
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    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).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM