0

I would like to write a c++ code that read a text file test.txt and write the conetent into several arrays. the file looks like:

[7,13,17]
[[0,1,1,0,0,1,0,0,1,0,0,0,0,0,1,0,0],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
[1,0,0,1,0,0,0,0,0,1,1,1,0,0,1,0,0],
[0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0],
[0,0,0,0,0,1,0,0,1,0,0,0,1,1,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0]]

the code i have written like

#include <iostream>
#include <fstream>

using namespace std;

int main()
{

  int parameter[10];
  int physical[7][7];

  const char* filename;

     filename = "src/cplex_N7.txt";

      ifstream f(filename, ios::in);
      if (!f) {
         cerr << "No such file: " << filename << endl;
         throw(1);
      }

    f >> parameter >> physical ;
  // cout  content
   return 0;
}

but i always got errors and wrong output, could you please help with doing this in the best way. I mean can i read the file without for loop or without handling the commas and splitters.

Waleed A
  • 95
  • 10
  • You are not handling the removal of all of the `[,],,` in the text – NathanOliver Jan 13 '16 at 19:38
  • You need to read each of the numbers individually, skipping delimitters and such as you go. – James Matta Jan 13 '16 at 19:39
  • 1
    You'll get more/better help if you spell out *what* errors, *what* the wrong output is and what it should be. – Scott Hunter Jan 13 '16 at 19:41
  • You have to read some books (Learning C++ by try and error is no good) –  Jan 13 '16 at 19:56
  • There are no builtin functions for reading entire arrays from a file in C++. You need to write your own suitable code or use a library. – davmac Jan 13 '16 at 20:05
  • thanks for your answer, yes, this is what i mean, whivh library i can use to read the content of the file directly in the variabless – Waleed A Jan 13 '16 at 20:09
  • @WaleedA Primary thing you need to implement is a loop. I've voted to close the question being _too broad_, because there are many many ways to to that actually. – πάντα ῥεῖ Jan 13 '16 at 20:12
  • @WaleedA Research a bit about parsing inputs. Asking for a library that does it for you is explicitly off-topic here. – πάντα ῥεῖ Jan 13 '16 at 20:15
  • Possible duplicate: http://stackoverflow.com/questions/572962/reading-a-matrix-txt-file-and-storing-as-an-array – Thomas Matthews Jan 13 '16 at 20:24
  • Possible duplicate [from "stackoverflow c++ read file matrix"](https://www.google.com/search?q=stackoverflow+c%2B%2B+read+file+matrix&ie=utf-8&oe=utf-8) – Thomas Matthews Jan 13 '16 at 20:25
  • Since you seem to interpret some values you see: you should **always** check whether input was successful before using the values, e.g., using `if (f >> parameter >> physical) { ... }`. – Dietmar Kühl Jan 13 '16 at 23:27

1 Answers1

0

you cant work with strings in this way. It seems that you are learning c++ with try/error method and it's the worst way to learn a programming language. I wrote a little sample to read a line of numbers in your format:

string x = "1,0,1,0,1,0,1,0,1,0";
int array[10];
for(int i = 0; i < x.size(); i+=2)
    array[i / 2] = x[i] - '0';

numbers are at even indexes so we should iterate on even poses of the string. each of them are a char and we can obtain the int value by subtracting them from '0'.

hamed1soleimani
  • 304
  • 1
  • 2
  • 13