Thread: How to verify a blank line in a file?

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    5

    Question How to verify a blank line in a file?

    I'm trying to verify that a file is in a very specific format.
    It should have a single number (n) on the first line, the second line is blank, each of the following lines have "R" or "W" followed by a number that's within a given range. There should be n non-blank lines after the first.
    The issue I'm having is with verifying the blank line. I check to see if fgets() of the line is NULL when the number of lines read is 1, but it seems to skip that line and gives the output "file formatting is invalid 4"

    Code:
    int reading = 1;
    int n = 0;
                    file = fopen(fileName, "r");
                    if (file == NULL) // check if the file exists
                    {
                        cout << "File not found" << endl;
                    }
                    else if (file != NULL) // if the file does exist
                    {
                        char fileLine[100];
                        int numIsolated = 432e6;
                        int numLinesRead = 0;
                        if (numLinesRead == 0)
                        {
                            fgets(fileLine, 100, file);
                            sscanf(fileLine, "%d", &numIsolated);
                            cout << endl
                                 << numIsolated << endl;
                            if (numIsolated > mmBytes || numIsolated < 0)
                            {
                                cout << "File not valid" << endl;
                                reading = 0;
                                break;
                            }
                            else
                            {
                                addArr[0] = numIsolated;
                            }
                        }
                        else if (numLinesRead == 1)
                        {
                            if (fgets(fileLine, 100, file) != NULL)
                            {
                                cout << "File invalid" << endl;
                                reading = 0;
                                break;
                            }
      
                        }
                        else
                        {
                            while (fgets(fileLine, 100, file) != NULL)
                            {
                                numAddresses = addArr[0];
                                char lineStr[100];
                                sscanf(fileLine, "%s %d", lineStr, &numIsolated);
                                cout << endl
                                     << string(lineStr) << endl;
                                cout << numIsolated << endl;
                                if (numIsolated > mmBytes || numIsolated < 0)
                                {
                                        cout << "File contains invalid address" << endl;
                                        reading = 0;
                                }
                                else
                                {
                                    if (string(lineStr) != "R" && string(lineStr) != "W")
                                    {
                                        cout << "file formatting is invalid 2" << endl;
                                        reading = 0;
                                        break;
                                    }
                                    else
                                    {
                                        n++;
                                        addArr[n] = numIsolated;
                                        printf("\naddArr[%d] = %d", n, addArr[n]);
                                        if (string(lineStr) == "R")
                                        {
                                            addArr[n + numAddresses] = 1;
                                            printf("\naddArr[%d] = %d", n + numAddresses, addArr[n + numAddresses]);
                                        }
                                        else
                                        {
                                            addArr[n + numAddresses] = 2;
                                            printf("\naddArr[%d] = %d", n + numAddresses, addArr[n + numAddresses]);
                                        }
                                    }
                                }
                                numLinesRead++;
                                numIsolated = 432e6;
                            }
                        }
                        if (reading == 1)
                        {
                            if (n != 0)
                            {
                                if (n != numAddresses)
                                {
                                    cout << "n = " << n << endl
                                         << "numAddresses = " << numAddresses << endl
                                         << "file formatting is invalid 3" << endl;
                                    reading = 0;
                                }
                                else if (n == numAddresses)
                                {
                                    validFile = 1;
                                    cout << endl
                                         << "n = " << n << endl
                                         << "numAddresses = " << numAddresses << endl
                                         << "arr[1] = " << addArr[1];
                                }
                            }
                            else
                            {
                                cout << "file formatting is invalid 4" << endl;
                                reading = 0;
                            }
                        }
    
                    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,914
    Use std::getline to read into a std::string instead, and then try to parse that string according to what should be on the line at that point.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,165
    A blank line returned by fgets() will consist of just a "\n".

    Why are you mixing C and C++?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2020
    Posts
    5
    @laserlight, I have tried that but am still having the same issue

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,914
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2020
    Posts
    5
    @Salem, I tried checking for "\n" as well but got the same result.

    And it's because this is my first project using either language so I'm not too familiar with the differences

  7. #7
    Registered User
    Join Date
    Apr 2020
    Posts
    5
    It is very similar to the original but I have replaced fgets() with getline().
    It is interesting because whenever I run the file through the simpler code below, it does print the "\n" character.


    Simple test code:

    Code:
    while (!feof(file))
        {
            characters = getline(&buffer, &bufsize, file);
            if (string(buffer) == "\n")
            {
                printf("You typed:\\n");
            }
            printf("%zu characters were read.\n", characters);
            printf("You typed: '%s'\n", buffer);
        }


    Current code:

    Code:
    int reading = 1;
                    int n = 0;
                    file = fopen(fileName, "r");
                    if (file == NULL) // check if the file exists
                    {
                        cout << "File not found" << endl;
                    }
                    else if (file != NULL) // if the file does exist
                    {
                        char fileLine[100];
                        int numIsolated = 432e6;
                        int numLinesRead = 0;
                        if (numLinesRead == 0)
                        {
                            char *buffer;
                            size_t bufsize = 32;
                            size_t characters;
    
                            buffer = (char *)malloc(bufsize * sizeof(char));
                            characters = getline(&buffer, &bufsize, file);
                            sscanf(buffer, "%d", &numIsolated);
                            cout << numIsolated << endl;
                            if (numIsolated > mmBytes || numIsolated < 0)
                            {
                                cout << "File not valid" << endl;
                                reading = 0;
                                break;
                            }
                            else
                            {
                                addArr[0] = numIsolated;
                            }
                        }
                        else if (numLinesRead == 1)
                        {
                            char *buffer;
                            size_t bufsize = 32;
                            size_t characters;
    
                            buffer = (char *)malloc(bufsize * sizeof(char));
                            characters = getline(&buffer, &bufsize, file);
                            if (string(buffer) != "\n")
                            {
                                cout << "File invalid" << endl;
                                reading = 0;
                            }
                        }
                        else
                        {
                            while (!feof(file))
                            {
                                numAddresses = addArr[0];
                                char *buffer;
                                size_t bufsize = 32;
                                size_t characters;
    
                                buffer = (char *)malloc(bufsize * sizeof(char));
                                characters = getline(&buffer, &bufsize, file);
                                char lineStr[100];
                                sscanf(buffer, "%s %d", lineStr, &numIsolated);
                                cout << endl
                                     << string(lineStr) << endl;
                                cout << numIsolated << endl;
                                if (numIsolated > mmBytes || numIsolated < 0)
                                {
                                    if (numLinesRead != 1)
                                    {
                                        cout << "File contains invalid address" << endl;
                                        reading = 0;
                                        break;
                                    }
                                }
                                else
                                {
                                    if (string(lineStr) != "R" && string(lineStr) != "W")
                                    {
                                        cout << "file formatting is invalid 2" << endl;
                                        reading = 0;
                                        break;
                                    }
                                    else
                                    {
                                        n++;
                                        addArr[n] = numIsolated;
                                        printf("\naddArr[%d] = %d", n, addArr[n]);
                                        if (string(lineStr) == "R")
                                        {
                                            addArr[n + numAddresses] = 1;
                                            printf("\naddArr[%d] = %d", n + numAddresses, addArr[n + numAddresses]);
                                        }
                                        else
                                        {
                                            addArr[n + numAddresses] = 2;
                                            printf("\naddArr[%d] = %d", n + numAddresses, addArr[n + numAddresses]);
                                        }
                                    }
                                }
                                numLinesRead++;
                                cout << endl
                                     << numLinesRead << endl;
                                numIsolated = 432e6;
                            }
                        }
                        if (reading == 1)
                        {
                            if (n != 0)
                            {
                                if (n != numAddresses)
                                {
                                    cout << "n = " << n << endl
                                         << "numAddresses = " << numAddresses << endl
                                         << "file formatting is invalid 3" << endl;
                                    reading = 0;
                                }
                                else if (n == numAddresses)
                                {
                                    validFile = 1;
                                    cout << endl
                                         << "n = " << n << endl
                                         << "numAddresses = " << numAddresses << endl
                                         << "arr[1] = " << addArr[1];
                                }
                            }
                            else
                            {
                                cout << "file formatting is invalid 4" << endl;
                                reading = 0;
                            }
                        }
                    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,914
    You cannot use getline because getline operates on an istream, not a FILE*.

    But why are you using C utitilies in a C++ program?

    For example, here is a program that verifies that each line in a file is a blank line:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    int main()
    {
        std::ifstream input("input.txt");
        bool all_blank = true;
        int line_no = 0;
        std::string line;
        while (getline(input, line))
        {
            ++line_no;
            if (!line.empty())
            {
                all_blank = false;
                std::cout << "Line #" << line_no << " is not blank!" << std::endl;
            }
        }
        if (all_blank)
        {
            std::cout << "All lines in the file are blank." << std::endl;
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 05-25-2012, 09:27 PM
  2. Checking for a Blank Line When Reading From a File
    By jeanermand in forum C Programming
    Replies: 6
    Last Post: 02-07-2012, 06:50 PM
  3. Reading a file containing a blank line
    By maniac123 in forum C++ Programming
    Replies: 8
    Last Post: 01-27-2011, 11:42 AM
  4. Help needed to verify a new on-line C test
    By JanHruska in forum Projects and Job Recruitment
    Replies: 15
    Last Post: 06-20-2009, 06:48 AM
  5. Replies: 3
    Last Post: 04-27-2005, 11:50 AM

Tags for this Thread