Checking whether file contains only whitespace C++

c string is empty or whitespace
isspace
bash check if file is empty or whitespace

I am trying to read input from a file in C++, and I need to give an error message if there is no input.

This statement works if the file is completely empty:

if (f.peek() == std::ifstream::traits_type::eof()) return error("Empty file");

where error() is a simple function:

int error(string message){
   cerr << "ERROR: " << message << "\n";
   return -1;
}

How can I check for files that contain only whitespace so I can raise the same error? Such as 7 newlines? My should continue executing normally if the file contains anything but only whitespace characters.

Scanning the file in advance if it only contains whitespaces would be very inefficient.

I believe what you really need, is to keep track if some non-whitespace data could be read at all, and raise the error if not:

  std::string line;
  std::vector<std::string> lines_with_data;
  while(std::getline(f,line)) {
      // check if line is empty or contains only whitespace
      if(!(line.empty() ||
          (std::find_if_not(line.begin(),line.end(),std::isspace) != line.end())) {
           lines_with_data.push_back(line);
      }          
  }
  if(lines_with_data.empty()) { // No data could be found
      return error("Empty file");
  }

How to check if file is empty or it has only blank characters?, Just grep for a character other than space: grep -q '[^[:space:]]' < "$file" && printf '​%s\n' "$file contains something else than whitespace characters". isspace() function. In C++, isspace is a predefined function used for string and character handling.cstring is the header file required for string functions and cctype is the headerfile required for character functions. This function is used to check if the arguement contains any whitespace characters.

reading characters and checking with std::isspace may help. something like this:

char c;
bool isEmpty = true;
while (f.get(c))
{
  if(!std::isspace(c))
  {
    isEmpty = false;
    break;
  }
}

this way, isEmpty will be what it says

Program to check if a String in Java contains only whitespaces , Process Synchronization · Deadlock · Memory Management · File and Disk Given a string str, the task is to check if this string contains only whitespaces or Then we can use the isEmpty() method of String class to check if the resultant C Program to Check if a Given String is Palindrome · Program to check if input is​  Checks whether c is a white-space character. Other locales may consider a different selection of characters as white-spaces, but never a character that returns true for isalnum. For a detailed chart on what the different ctype functions return for each character of the standard ASCII character set,

You could use an std::istream_iterator over the file input and check individual characters with std:isspace(), I suppose.

But I wonder if this is really what you want to do. Why not try to read whatever it is you want to read, and only worry about things when you fail your reading/parsing?

C#, Basics Operations of File and Directory in C# · C# Dynamic Coding It is used to check whether the specified string is null or contains only white-space  C# has two very under appreciated helper methods that can be used to quickly determine whether a string is null, empty, or just contains whitespace. To determine whether a C# String is empty (Length == 0) or null, you can use the static string helper method string.IsNullOrEmpty().

How to check if string is empty or has only spaces in it using Perl?, To check if string is empty use eq. To check if it has only spaces or only white space in it, use a regex. Are you serious about Perl? Check  Check the buffer to see if it contains only a newline (or alternately, only whitespace and a newline). Or fgets + sscanf, and see if sscanf returns what you expect. Quzah. Hope is the first step on the road to disappointment.

How to check if text is “empty” (spaces, tabs, newlines) in Python?, The string can be checked by checking for occurrences of only whitespace characters. We can check if a string contains only whitespace  Checks if the String contains only whitespace. /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership.

Checking for Empty or Blank Strings in Java, How do you check if a string is just spaces? check if line is whitespace. C / C++ Forums on Bytes. What is the quickest way to check that the following: const line[127]; only contains whitespace, in which

Comments
  • You have to read and inspect every character then. std::isspace() may come in handy to do that.
  • What are the chances of a file containing only whitespace? This seems like an XY problem.
  • @Neil True that.
  • Then you need some code to illustrate your problem. Reading the entire file, before doing any useful processing, to check for whitespace is not sensible.
  • Sorry for not being clear, I was asking if there was a way to check in a similar fashion to checking for an empty file but the real solution was checking after parsing text. You are right about this being an XY problem, now that I am familiar with this term I will ask properly next time. Thank you
  • Thank you, this approach worked (checking after parsing) and I was mistaken trying to check in a similar manner to checking for an empty file.