CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 1999
    Posts
    27

    Reading Text Files

    Hi, I'm new to Visual C++, and I'm trying to learn to work with text files. I'm doing pretty good so far with the help of the MSDN Library. In my program that I'm making just to help me learn about reading and writing to text files, I've run into a problem. The program is supposed to read a text file line by line, and make changes to it based on what's there. I also need the program to skip to the next line when the two characters '//' are encountered. Here's what I've got so far:

    void CParseDataApp::OnOpenText()
    {
    FILE *textFile;
    char getText[100];
    char firstTwo[3];
    CString fileName = "C:\\Doug\\Programs\\R6\\test.txt";
    textFile = fopen(fileName,"r+");
    if (textFile != NULL)
    {
    int counter = 0;
    while (counter != 4)
    {
    fgets(firstTwo,3,textFile);
    if (firstTwo != "//")
    {
    fgets(getText,99,textFile);
    MessageBox(NULL,firstTwo,"Result",MB_ICONASTERISK);
    counter++;
    } else {
    MessageBox(NULL,firstTwo,"Comment",MB_ICONSTOP);
    counter++;
    }
    }
    } else {
    MessageBox(NULL,"The text file "+fileName+"\ncould not be opened.","Error",MB_ICONSTOP);
    }
    }



    the text file looks like this:

    //
    **
    ^^
    hey hey

    Ok, now here's the problem. When the program is run, it doesn't recognise the first line '//' as a comment. It's like the statement

    if (firstTwo != "//")



    doesn't work. Is it because the variable firstTwo is a char? When run, I get four MessageBoxes, each one having an asterisk as an icon, each one from the 'if' bracket of the statement. But yet the first line of the text file is "//". The messageBox even tells me it's "//". Yet it doesn't execute the 'else' bracket of the statement. Does anyone know why not? This gets really frustrating when everything looks right, but it doesn't work. If anyone has a guess as to why this doesn't work, please let me know. Any help is greatly appreciated. Thanks!


  2. #2
    Join Date
    Apr 1999
    Posts
    23

    Re: Reading Text Files

    Hi,

    The variable first two is a pointer to a character, and you tried to compare it to a string in the if statement. Try modifying your code to:

    void CParseDataApp::OnOpenText()
    {
    FILE *textFile;
    char getText[100];
    char firstTwo[3];
    CString fileName = "C:\\Doug\\Programs\\R6\\test.txt";
    textFile = fopen(fileName,"r+");
    if (textFile != NULL)
    {
    int counter = 0;
    while (counter != 4)
    {
    fgets(firstTwo,3,textFile);
    if (firstTwo[0] != '/' && firstTwo[1]!='/')
    {
    fgets(getText,99,textFile);
    MessageBox(NULL,firstTwo,"Result",MB_ICONASTERISK);
    counter++;
    } else {
    MessageBox(NULL,firstTwo,"Comment",MB_ICONSTOP);
    counter++;
    }
    }
    } else {
    MessageBox(NULL,"The text file "+fileName+"\ncould not be opened.","Error",MB_ICONSTOP);
    }
    }

    Hope this is helpful

    Chao


  3. #3
    Join Date
    May 1999
    Location
    Antwerp, Belgium
    Posts
    136

    Re: Reading Text Files

    use the strcmp function to compare char-pointers.

    Your program doesn't work if the '//' are not at the beginning of a line. You can read a line and then use strstr to find the substring '//'.

    Another tip : see to the getline function. It's a function defined in STL. You don't have to worry anymore about the linelength. STL will do it for you. You can also use the string-implementation of STL.Of course this means you have to dig in STL and that's not always easy. A very good startpoint for this is reading the chapters of Thinking In C++, which you can find here at Codeguru.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured