Detecting comments in file input not working

so I have a program that reads from a file that looks like this:

1
2
3
hello-world .txt
hello, world! this is the content
$stopf


the first part ('hello-world') is the title of the file that my program is going to create. the second part ('.txt') is the extension of the file. the final part ('$stopf') is the command to stop writing to the file. everything that is between the second and final part is the content of the file.

now what I wanted to do is add comments, so it would look like this:

1
2
3
4
5
hello-world .txt #this is valid and is a comment too
hello, world! this is the content
#this is a comment
this is not :)
$stopf


the comments, as you would expect do not appear in the file but there is a ghost line, so the output would be:

1
2
3
4

hello, world! this is the content

this is not :)


this is problem 1, and problem 2 is how can I fix this bug:

1
2
3
hello-world .txt
hello, world! #this still outputs in the file
$stopf


and the output in the file is:

 
hello, world! #this still outputs in the file 



the program looks like this:

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
29
30
31
32
33
34
35
36
37
38
int edit(){

    //the editor
    editor >> name >> ext;
    name += ext;
    std::string path = name_dir + "\\" + name;
    std::ofstream out(path);
    bool write = true;
    //reads the file
    while(getline(editor, n)){
        write = true;

        //new file command
        if(n == "$stop")
            edit();

        //stop editing command
        if(n == "$stopf"){
            return 0;
        }

        //read more characters
        while(n.length() == 0)
            getline(editor, n);

        //comment
        if(n[0] == '#' ||
            //in case you comment after an extension declaration
          (n[0] == ' ' && n[1] == '#'))
            write = false;

        //write the string
        if(!editor.eof() && write == true)
            if(n[0] != '#') out << n << " ";
        out << '\n';
    }
    editor.close();
}
This may not be exactly what you're looking for, but maybe it will give you some ideas.
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
29
30
31
32
33
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;

int main() {
    ifstream fin("edit.txt");
    ofstream fout;
    string line;
    bool need_name = true;

    while (getline(fin, line)) {
        istringstream sin(line);
        if (need_name) {
            string name, ext;
            sin >> name >> ext;
            fout.open(name + ext);
            need_name = false;
        }
        else {
            if (line == "$stop") {
                fout.close();
                need_name = true;
            }
            else if (line == "$stopf") {
                fout.close();
                break;
            }
            fout << line.substr(0, line.find('#')) << '\n';
        }
    }
}


Input file:

hello-world .txt #this is valid and is a comment too
hello, world! this is the content
#this is a comment
this is not :)
$stop
another-file .txt
more content # another comment?
    # comment (what to do with the spaces?)
last line
$stopf

Topic archived. No new replies allowed.