Skip to content

Commit 00ac8a8

Browse files
committed
Added ReadFromFile cpp method.
Added test.
1 parent 2a7dc32 commit 00ac8a8

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
#include <fstream>
4+
#include <iostream>
5+
#include <sstream>
6+
#include <string>
7+
8+
namespace RhythmGameUtilities
9+
{
10+
11+
std::string ReadFromFile(const char *path)
12+
{
13+
std::ifstream file(path);
14+
15+
if (!file)
16+
{
17+
std::cerr << "Failed to open " << path << "." << std::endl;
18+
19+
return "";
20+
}
21+
22+
return std::string((std::istreambuf_iterator<char>(file)),
23+
std::istreambuf_iterator<char>());
24+
}
25+
26+
} // namespace RhythmGameUtilities

tests/Mocks/test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello, world!

tests/RhythmGameUtilities/File.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <cassert>
2+
#include <iostream>
3+
4+
#include "RhythmGameUtilities/File.hpp"
5+
6+
using namespace RhythmGameUtilities;
7+
8+
void testReadFromFile()
9+
{
10+
assert(ReadFromFile("./tests/Mocks/test.txt") == "Hello, world!\n");
11+
12+
std::cout << ".";
13+
}
14+
15+
int main()
16+
{
17+
testReadFromFile();
18+
19+
return 0;
20+
}

0 commit comments

Comments
 (0)