Skip to main content
edited title
Link
Tong Zhao
  • 101
  • 1
  • 10

How to read from a file in C++ during(during a GoogleTest)

Post Closed as "Duplicate" by Chris, Drew Dormann c++
added 104 characters in body
Source Link
Tong Zhao
  • 101
  • 1
  • 10

UPDATED: I am learning about GoogleTest to test C/C++ programs. I heard GoogleTest is a good tool for it. I tried one simple thing, which is to read input from a file, during a TEST() function, and it did not seem to work:

test.cpp in the google test project:

#include <iostream> #include <fstream> #include <filesystem> TEST(IOTest, ReadFile) { std::string filename = "input.txt"; std::string buffer; std::ifstream ifs(filename, std::ios::binary); std::filesystem::path cwd = std::filesystem::current_path(); std::cout << "Current directory path: " << cwd << std::endl; std::cout << "Current file path: " << __FILE__ << std::endl; ASSERT_TRUE(ifs.is_open()); } 

The resulting test failed. And the output is:

Current directory path: "C:\\Users\\Tong\\source\\repos\\BookLibrary\\Debug" Current file path: C:\Users\Tong\source\repos\BookLibrary\GoogleTest\test.cpp Value of: ifs.is_open() Actual: false Expected: true 

I placed the file "input.txt" under the current directory, which is:

C:\Users\Tong\source\repos\BookLibrary\GoogleTest\Debug\input.txt 

but it seems that the program cannot find or open it.

Am I missing something here? I also tried

std::string filename = "./input.txt"; 

but still no luck in reading the file.

UPDATE: problem solved; everything was correct except that I had placed the input.txt file in the wrong folder. It should be placed in

C:\Users\Tong\source\repos\BookLibrary\Debug 

and not

C:\Users\Tong\source\repos\BookLibrary\GoogleTest\Debug 

UPDATED: I am learning about GoogleTest to test C/C++ programs. I heard GoogleTest is a good tool for it. I tried one simple thing, which is to read input from a file, during a TEST() function, and it did not seem to work:

test.cpp in the google test project:

#include <iostream> #include <fstream> #include <filesystem> TEST(IOTest, ReadFile) { std::string filename = "input.txt"; std::string buffer; std::ifstream ifs(filename, std::ios::binary); std::filesystem::path cwd = std::filesystem::current_path(); std::cout << "Current directory path: " << cwd << std::endl; std::cout << "Current file path: " << __FILE__ << std::endl; ASSERT_TRUE(ifs.is_open()); } 

The resulting test failed. And the output is:

Current directory path: "C:\\Users\\Tong\\source\\repos\\BookLibrary\\Debug" Current file path: C:\Users\Tong\source\repos\BookLibrary\GoogleTest\test.cpp Value of: ifs.is_open() Actual: false Expected: true 

I placed the file "input.txt" under the current directory, which is:

C:\Users\Tong\source\repos\BookLibrary\GoogleTest\Debug\input.txt 

but it seems that the program cannot find or open it.

Am I missing something here?

UPDATED: I am learning about GoogleTest to test C/C++ programs. I heard GoogleTest is a good tool for it. I tried one simple thing, which is to read input from a file, during a TEST() function, and it did not seem to work:

test.cpp in the google test project:

#include <iostream> #include <fstream> #include <filesystem> TEST(IOTest, ReadFile) { std::string filename = "input.txt"; std::string buffer; std::ifstream ifs(filename, std::ios::binary); std::filesystem::path cwd = std::filesystem::current_path(); std::cout << "Current directory path: " << cwd << std::endl; std::cout << "Current file path: " << __FILE__ << std::endl; ASSERT_TRUE(ifs.is_open()); } 

The resulting test failed. And the output is:

Current directory path: "C:\\Users\\Tong\\source\\repos\\BookLibrary\\Debug" Current file path: C:\Users\Tong\source\repos\BookLibrary\GoogleTest\test.cpp Value of: ifs.is_open() Actual: false Expected: true 

I placed the file "input.txt" under the current directory, which is:

C:\Users\Tong\source\repos\BookLibrary\GoogleTest\Debug\input.txt 

but it seems that the program cannot find or open it.

Am I missing something here? I also tried

std::string filename = "./input.txt"; 

but still no luck in reading the file.

UPDATE: problem solved; everything was correct except that I had placed the input.txt file in the wrong folder. It should be placed in

C:\Users\Tong\source\repos\BookLibrary\Debug 

and not

C:\Users\Tong\source\repos\BookLibrary\GoogleTest\Debug 
added 200 characters in body
Source Link
Tong Zhao
  • 101
  • 1
  • 10

UPDATED: I am learning about GoogleTest to test C/C++ programs. I heard GoogleTest is a good tool for it. I tried one simple thing, which is to read input from a file, during a TEST() function, and it did not seem to work:

test.cpp in the google test project:

#include <iostream> #include <fstream> #include <filesystem> TEST(IOTest, ReadFile) { std::string filename = "input.txt"; std::string buffer; std::ifstream ifs(filename, std::ios::binary); std::filesystem::path cwd = std::filesystem::current_path(); std::cout << "Current folderdirectory path: " << cwd << std::endl; std::cout << "Current file path: " << __FILE__ << std::endl; ASSERT_TRUE(ifs.is_open()); } } 

The resulting test failed. And the output is:

Current folderdirectory path: "C:\\Users\\Tong\\source\\repos\\BookLibrary\\Debug" Current file path: C:\Users\Tong\source\repos\BookLibrary\GoogleTest\test.cpp   Value of: ifs.is_open() Actual: false Expected: true 

I placed the file "input.txt" under the project folder of my google test projectcurrent directory, which is:

C:\Users\Tong\source\repos\BookLibrary\GoogleTest\input\Users\Tong\source\repos\BookLibrary\GoogleTest\Debug\input.txt 

but it seems that the program cannot find or open it.

Am I missing something here?

UPDATED: I am learning about GoogleTest to test C/C++ programs. I heard GoogleTest is a good tool for it. I tried one simple thing, which is to read input from a file, during a TEST() function, and it did not seem to work:

TEST(IOTest, ReadFile) { std::string filename = "input.txt"; std::string buffer; std::ifstream ifs(filename, std::ios::binary); std::cout << "Current folder: " << __FILE__ << std::endl; ASSERT_TRUE(ifs.is_open()); } } 

The resulting test failed. And the output is:

Current folder: C:\Users\Tong\source\repos\BookLibrary\GoogleTest\test.cpp Value of: ifs.is_open() Actual: false Expected: true 

I placed the file "input.txt" under the project folder of my google test project, which is:

C:\Users\Tong\source\repos\BookLibrary\GoogleTest\input.txt 

but it seems that the program cannot find or open it.

Am I missing something here?

UPDATED: I am learning about GoogleTest to test C/C++ programs. I heard GoogleTest is a good tool for it. I tried one simple thing, which is to read input from a file, during a TEST() function, and it did not seem to work:

test.cpp in the google test project:

#include <iostream> #include <fstream> #include <filesystem> TEST(IOTest, ReadFile) { std::string filename = "input.txt"; std::string buffer; std::ifstream ifs(filename, std::ios::binary); std::filesystem::path cwd = std::filesystem::current_path(); std::cout << "Current directory path: " << cwd << std::endl; std::cout << "Current file path: " << __FILE__ << std::endl; ASSERT_TRUE(ifs.is_open()); } 

The resulting test failed. And the output is:

Current directory path: "C:\\Users\\Tong\\source\\repos\\BookLibrary\\Debug" Current file path: C:\Users\Tong\source\repos\BookLibrary\GoogleTest\test.cpp   Value of: ifs.is_open() Actual: false Expected: true 

I placed the file "input.txt" under the current directory, which is:

C:\Users\Tong\source\repos\BookLibrary\GoogleTest\Debug\input.txt 

but it seems that the program cannot find or open it.

Am I missing something here?

added 261 characters in body
Source Link
Tong Zhao
  • 101
  • 1
  • 10
Loading
code improved but still not right
Source Link
Tong Zhao
  • 101
  • 1
  • 10
Loading
Source Link
Tong Zhao
  • 101
  • 1
  • 10
Loading