-1

So I'm trying to overwrite a macro I have on a header file but I can't seem to open it by using std::ifstream. Is it even possible to read/write to an existing header file or are there default permissions that don't allow programs to modify header file contents?

std::ifstream versionH;
char temp[100];
versionH.open("..\temp.h");
if (!versionH.is_open()) {
    std::cout << "Didn't open" << std::endl;
    return 1;
}
while (!versionH.eof()) {
    versionH >> temp;
    std::cout << temp << std::endl;
}

I would hope that I'd be able to read in the header file and display it's contents but 'versionH.is_open()' is returning false. Is there something I'm missing here?

  • 1
    That back-tick should be escaped as as a double back-tick: `..\\temp.h`. And note, the only way that "works" (term used very loosely) is if the current working directory at runtime is one level deeper than the location of `temp.h` on disk. Somehow, I think that the least of your problems to come, but that's probably the problem with *this* code. – WhozCraig May 28 '19 at 16:04
  • 1
    There should not be a problem doing this. **BUT** self modifying code is not a good idea. **ALSO** that while loop is wrong. Look at: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/q/5605125/14065) – Martin York May 28 '19 at 16:06
  • WhozCraig suggested a fix by escaaping the backslash. **OR** you cuold use the slash that is supported on all systems nowadays: **"../temp.h"** Also `versionH >> temp;` reads a space separated word (not a line). Try `std::getline()` and prefer `std::string temp;` to `char temp[100];`. – Martin York May 28 '19 at 16:08
  • 1
    A header file is a file like any other. Of course you can write to it. The question is whether you *should*. There's probably a better way to accomplish whatever you are trying to do. – Jesper Juhl May 28 '19 at 16:13
  • @WhozCraig Wow it worked, what a simple fix. Thank you! – Neema Nejad May 28 '19 at 16:21
  • @MartinYork Thanks man, I changed the while check based on your suggestion. – Neema Nejad May 28 '19 at 16:31

1 Answers1

1

Is it possible to read and write to a header file?

Headers are files. It is possible to read and write files (assuming the file exists, and the process has sufficient permissions etc.). Therefore we can infer that header files can be read and written to.

Note that modifying a header file that has been used to compile a program has no effect on the compiled program. It can only affect programs compiled using the modified file.

Furthermore, files in the context where the program is compiled are irrelevant to the program. Only the files in the file system where the program is executed can be read.

Is there something I'm missing here?

Probably the file doesn't exist. The filename is most suspicious. Does it really contain a tab character (\t), or did you intend to write a (windows) dir separator? The backslash is the escape character, so in order to write it in a string literal, you must escape it (with another backslash: \\).

eerorika
  • 181,943
  • 10
  • 144
  • 256