C++ why you no work???

trying to read from this input and print it out to stdout:

. . * . . . . . . . . . . . . . . . . .
. . . * . . . . . . . . . . . . . . . .
. . . * . A . . . . . . . . * * * . . .
. . . . * . . . . . . . . . * B . . . .
. . . . . * . . . . . . . . * * * . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . * * . . . . . . . . . . . . *
. . . . . . . * * . . . . . . . . . * .
. . . C . . . . . * . . . . . . . * . .
. . . . . . . . . . * . . . . . . . * .
. . . . * * * * . . . . . . . . . . * .
. . . . * * * * . . . * . . . . . . . .
. . . * . . . . . . . . * * . . * . . .
. . . * . . . . . . . . . . * * . . . .
. . . * . . . . D . . . . . . . . . . .
. . . * . . . . . . . . . . . . . . . .
. . . * . . . . . . . . . . . . . . . .
. . . * . . . . . . . . . . . . . . . .
. . . * . . . . . . . . . . . . E . . .
. . . . . . . . . . . . . . . . . . . .




Here is my code:
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
#include <cstdio>

FILE* InputFile;

//struct CityBlock
//{
//    bool Blocked;
//    bool N_Blocked;
//    bool FireStation;
//    CityBlock* right;
//    CityBlock* left;
//};

int main(int argc, char * argv[])
{
    if (argc < 2)
        puts("You need to provide a file as argument!\n");
    else
    {
        InputFile = fopen(*argv, "r");
        char ** Map = new char*[20];
        
        if (NULL != InputFile)
        {
            puts("The file is good to go!\n");
            for (int R = 0; !feof(InputFile); ++R)
            {
                Map[R] = new char[20];
                fgets(Map[R], 100, InputFile);
                for (int T = 0; T < 20; ++T)
                    if (fscanf(InputFile, "%c", &Map[R][T]))
                        printf("%c", Map[R][T]);
            }
        }
        fclose(InputFile);
    }
    return 0;
}


it outputs whole lot of nonsense
snippet:
8?/usr/lib/libSystem.B.dylib&X)`!jH??H???H?H?u??????
H?H???H?H?9u?H??<????????UH??H??H?}?H?H???wH??]???
UH??H?? ?}?H?u??E???H??H???l?H??E???????H??H?????????
????M?M??u?H?gH????E??E?E?E?H?? ]ÐUH??????lj??


Can someone explain to me where I am going wrong in this one?
Last edited on
If i understand that correctly, you are creating a char array with 20 elements, but would allow reading up to 100 characters into it. If there is a proper newline before 21 chars, then that isn't the problem now, but it is a flaw in the program.

What's really weird is that you are reading characters with fgets, and then immediately reading even more into the same array with scanf.

Another flaw is that you might read more than 20 lines and overflow your array. I'm not really sure what you are trying to do.
I think that the problem is in your call to fopen - *argv is the same as argv[0] and argv[0] is always the address of your program - so you're actually reading from the executable.

Calling fopen(argv[1],"r") will fix this by opening the input file, which is the second element of the array argv.

EDIT:
Also, I would suggest using <fstream> and <iostream> instead of <cstdio> - they will automatically ignore whitespaces and newlines. fscanf won't necessarily do that.
Last edited on
Running the following code should indicate what the problem is.

1
2
3
4
5
6
#include <iostream>

int main( int argc, char* argv[])
{
    std::cout << *argv << '\n' ;
}
BOOM!

Everyone who answered. You guys are awesome! Thank you much much :D
Topic archived. No new replies allowed.