-1

I'm trying to bring data from .txt file for my program that makes edge adjacency list using C++, but it didn't work the way I wanted. I don't have good knowledge in regards to delimiters of ifstream >> function.

I know that ifstream >> ignores line segments and reads the next value, but I'm not sure if it also ignores spaces.

int numVert;
int numEdge;

int src, dest, weight;

ifstream myFile("Ginput.txt");
myFile >> numVert;
myFile >> numEdge;

graph = createGraph(numVert);

for (int i = 0; i <= numEdge*3; i++)
{
    myFile >> src;
    myFile >> dest;
    myFile >> weight;
    addEdge(graph, src, dest, weight);
}

The .txt file format is like this:

3 6
1 4 2
2 4 6
2 3 2
1 2 3
2 5 6
2 1 5
1 4 3

The first 2 integers are for number of vertices and number of edges, respectively.

The first digit, after the first line, is the source, second digit is destination, and third digit is weight.

What it should do is to distinguish between lines and spaces, and input the integers into correct data. So, it should be:

numVert = 3;
numEdge = 6;
First line: src = 1, dest = 4, weight = 2
Second line: src = 2, dest = 4, weight = 6

and so on.

Please help me out. Thank you.

Brandon J
  • 3
  • 1
  • 2
    The file you show says that there are `6` edges but then there are 7 triplets of numbers (edges). And you attempt to read `6 * 3 + 1` (i.e. 19) triplets (edges). – Some programmer dude Apr 12 '19 at 08:12
  • It should distinguish them with no problem, see some useful listing: https://en.cppreference.com/w/cpp/string/byte/isspace – hauron Apr 12 '19 at 08:13
  • 2
    `istream`s operator `>>` generally skips over whitespace entirely. Whitespace includes both space characters and new line markers (among other things) so they are ignored equally. The value of `numEdge` read from the file is 6, and your code loops from 0 to `numEdge*3` inclusive i.e. 19 times. Your file does not contain 19 lines – Peter Apr 12 '19 at 08:13

1 Answers1

2

To activate skipping whitespace for operator>> you would insert:

myFile >> std::skipws;

but this is activated by default. So what does std::skipws skip now? The answer is anything that is true on std::isspace including the newline '\n' and spaces ' ' meaning you do not have to distuingish between those.

You have one mistake in your program which is that you fill 3 Inputs but loop i < numEdge*3 which results in filling numEdge*9 onputs, which is too much.

You used non-mcve statements like graph = createGraph(numVert); and addEdge(graph, src, dest, weight); so I excluded those from the code. You also shouldn't use namespace std;.

#include <iostream>
#include <fstream>

int main() {
    int numVert;
    int numEdge;

    int src, dest, weight;

    std::ifstream myFile("Ginput.txt");
    myFile >> numVert;
    myFile >> numEdge;

    myFile >> std::skipws;

    std::cout << "numVert = " << numVert << '\n';
    std::cout << "numEdge = " << numEdge << '\n';

    //graph = createGraph(numVert);

    for (int i = 0; i <= numEdge; i++){
        myFile >> src;
        myFile >> dest;
        myFile >> weight;
        std::cout << "line " << i + 1 << " : src = " << src << ", dest = " << dest << ", weight = " << weight << '\n';

        //addEdge(graph, src, dest, weight);
    }
}

output:

numVert = 3
numEdge = 6
line 1 : src = 1, dest = 4, weight = 2
line 2 : src = 2, dest = 4, weight = 6
line 3 : src = 2, dest = 3, weight = 2
line 4 : src = 1, dest = 2, weight = 3
line 5 : src = 2, dest = 5, weight = 6
line 6 : src = 2, dest = 1, weight = 5
line 7 : src = 1, dest = 4, weight = 3
Stack Danny
  • 7,754
  • 2
  • 26
  • 55
  • I believe you nailed it but I would spent a side note about `numEdge`. My first inconvenience was about `for (int i = 0; i <= numEdge; i++)` (which looks like a typical beginners error) and I scrolled for and back until I understood that the condition is correct. May be, it would be much clearer when it is renamed to something like `iMaxEdge`... ;-) – Scheff's Cat Apr 12 '19 at 08:37
  • yes @Scheff I agree. Also `numEdge = 6` but the number of lines `= 7`. Would be an improvement to make `numEdge = 7` and change `i <=` to `i <` – Stack Danny Apr 12 '19 at 08:40
  • `numEdge = 6` resulting in `line 1` to `line 7` was the next thing I was about to mention. ;-) I'm not sure where the input format does come from, and OP might not be allowed to change. But I agree that's something else I would consider as unlucky design. – Scheff's Cat Apr 12 '19 at 08:42
  • A lot of people mentioned that the numEdge should read line by line, so I already made a change to that. I've tested some cases, and I think there was an error in my main.cpp. Thanks for the help though! – Brandon J Apr 12 '19 at 16:21