Please help me figure out what I'm doing wrong here

I'm making my own programming language, I have some experience with that in Python and Java. But I tried doing it in C++ and now I have an issue with reading files. Someone please help me figure out why, the source code is on https://github.com/Axocudo/Axocudo.
Last edited on
That link gives 'Page not found' error 404.
I have an issue with reading files. Someone please help me figure out why
I checked out your code and built it, now what?

Is there a reason STRING is defined to be char*, rather than std::string?

I added warnings to the makefile and rebuilt, you have a lot of relevant warnings. You should look into that. Add these flags to your compile line: -Wall -Wextra -g.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
15├>STRING getFileContents(const STRING filepath) {
16│     std::string line;
17│     std::string text;
18│     std::ifstream myfile (filepath);
19│
20│     if (myfile.is_open()) {
21│         while (std::getline(myfile, line)) {
22│             text += line + "\n";
23│         }
24│
25│         myfile.close();
26│     }
27│
28│     STRING c = const_cast<STRING>(text.c_str()); // convert std::string to char*
29│     return c;

This is the first function I stepped into. Knowing that STRING is char*, the compiler's tried to warn you about line 28, and you shut it up with that cast. But now you're left to deal with the problem it warned you about.

I suggest you document your project aims, and what some code might look like. Then you'll be in a position to get some help. At the moment, there's nothing I can do beyond what I've said here.

Also, it might be best to move the topic to General C++ Programming as this isn't Windows specific.
Last edited on
Is there a reason STRING is defined to be char*, rather than std::string?

Indeed there is, I plan to use this to allow mods to be made for my game, which is made using GameMaker Studio 2, which only supports char* and double as arguments and return values.

I added warnings to the makefile and rebuilt, you have a lot of relevant warnings. You should look into that. Add these flags to your compile line: -Wall -Wextra -g.

I have added these flags and resolved all the currently showing warnings.

This is the first function I stepped into. Knowing that STRING is char*, the compiler's tried to warn you about line 28, and you shut it up with that cast. But now you're left to deal with the problem it warned you about.

I didn't get any warning from this.

Also, it might be best to move the topic to General C++ Programming as this isn't Windows specific.

I'll do that right now!
L28. c_str() returns a non-modifiable standard C character array version of the string. Don't cast to a non-const. Modifying the character array accessed through the const cast is undefined. From C++17, if you want a modifiable pointer than use .data(). Note that prior to C++17, .data() also returned a non-modifiable pointer.
Yay now my code just freezes :/
L21 22., Note that this is the very slow way to read the whole of a text file.

A better way is to use .get() to read the whole file with one statement. Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Reads whole file into memory
// fn - file name to use
// fd - string to contain file contents
// returns true if read OK. False otherwise

bool readFile(const std::string& fn, std::string& fd)
{
	using fsztp = std::string::size_type;

	bool ret {false};
	std::ifstream fs(fn);

	if (fs.is_open()) {
		fs.seekg(0, fs.end);

		const auto fsz = static_cast<std::streamoff>(fs.tellg());

		if (fs && (fsz <= std::numeric_limits<fsztp>::max())) {
			fd.resize(static_cast<fsztp>(fsz));
			fs.seekg(0);
			fs.read(reinterpret_cast<char*>(fd.data()), fsz);
			fd.resize(static_cast<fsztp>(fs.gcount()));
			ret = true;
		}
	}

	return ret;
}

Last edited on
You are ultimately returning a pointer to a (member subobject of a) local variable.
int* f() { int x = 0; return &x; } // exhibits same problem
The function f always returns a "dangling pointer", a pointer to an object whose lifetime has already ended.

https://en.cppreference.com/w/cpp/language/lifetime

(I couldn't tell if OP identified this problem already)
Last edited on
The issue has been resolved by modifying almost all the files (the exceptions are 2 header and 2 source files) to use std::string instead of char*
Update: It's not solved at all, I'll make a new thread for this tho as it isn't related to filereading. New thread: https://www.cplusplus.com/forum/general/279318/
Last edited on
Topic archived. No new replies allowed.