Hey guys,
I am posting here for the first time, so please be gentle with me :P. I have an assignment of storing a text file in two arrays (one contains both char and int, whereas the other contains just numbers. The text file looks something like this:

Time 2 3 35
Sub 16 85 0 49

Since I am new to programming (not really: Studied structures five years ago), I am having difficulty storing this data into the array. The only thing which I was able to do was to read the data and display it on the screen. Can anyone please help me with the writing part please?

My code looks something like this:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  char myarray[200];
  char index=0;
  ifstream myfile ("file.txt");
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,line);
      cin>> line;
    }
    myfile.close();
  }

  else cout << "Unable to open file";

  return 0;
}

Any help would be appreciated.

What does the file represent? You can certainly break the parts into an array, but a better approach would be to create a record structure with some sort of application-specific abstraction.

Okay. My main objective is to design a circuit simulator using C++. One of the sub-tasks is to create two arrays and store gate information in one array (what drives the gate, gate drives what?, what type of gate is driven). The second array stores the 'nets(wires)' information. The text file looks like this (just a sample with random gates):

NOT 1 2
AND 3 5 6
INPUT 3 6 7 8
OUTPUT 4 9 11 23

Here the first or the first two integers are the input logic of the gate. The second/third value is the output.
What I am planning to do is something like this:

1. Read the data from the text file.
2. Write the data in two separate arrays.
3. In the first array, the size would be the number of nets, and the information would be the logic value on the net.
4. The second array will store the gates data. The size would be the lines in the text file, and the data will be the gates information (which net/nets drive the gates, which net does the gate drive)
5. Then The user will give a set of initial net values.
6. After this, the net value will be updated in the array.
7. Then this information will be used to perform the logical operations mentioned in the gate array. ( If any data is miising then the operation won't be performed).
8. The output of the operation will be used to update the net value in array 1.
9. This will be repeated till all the operations have been performed.

Phew. I think I'll be done by then :P
For clearer explanation I am adding this link : http://www.cs.princeton.edu/courses/archive/fall2000/cs126/assignments/circuit.html

This is not my assignment though. I don't have the clock values.

Here's a simple example that'll work with a ascii text file.

#include <iostream>
#include <fstream>

struct mys
{
	char ca[10];
	unsigned long a, b;
} thes;

std::ifstream& operator >>(std::ifstream & in, struct mys & s)
{
	in >> s.ca;
	in >> s.a;
	in >> s.b;
	return in;
}

int main(int argc, char**argv)
{
	std::string filename;
	std::cout << "Enter filename->";
	std::cin >> filename;

	std::ifstream myfile (filename.c_str());

	myfile >> thes;

	std::cout << "ca->" << thes.ca << " a->" << thes.a << " b->" << thes.b << std::endl;
	
	myfile.close();
	
	return 0;
} 

/* data file

one 44 45
two 34 87
three 56 87
four 34 56


*/

What does the file represent? You can certainly break the parts into an array, but a better approach would be to create a record structure with some sort of application-specific abstraction.

Okay. My main objective is to design a circuit simulator using C++. One of the sub-tasks is to create two arrays and store gate information in one array (what drives the gate, gate drives what?, what type of gate is driven). The second array stores the 'nets(wires)' information. The text file looks like this (just a sample with random gates):

NOT 1 2
AND 3 5 6
INPUT 3 6 7 8
OUTPUT 4 9 11 23

Here the first or the first two integers are the input logic of the gate. The second/third value is the output.
What I am planning to do is something like this:

1. Read the data from the text file.
2. Write the data in two separate arrays.
3. In the first array, the size would be the number of nets, and the information would be the logic value on the net.
4. The second array will store the gates data. The size would be the lines in the text file, and the data will be the gates information (which net/nets drive the gates, which net does the gate drive)
5. Then The user will give a set of initial net values.
6. After this, the net value will be updated in the array.
7. Then this information will be used to perform the logical operations mentioned in the gate array. ( If any data is miising then the operation won't be performed).
8. The output of the operation will be used to update the net value in array 1.
9. This will be repeated till all the operations have been performed.

Phew. I think I'll be done by then :P
For clearer explanation I am adding this link : http://www.cs.princeton.edu/courses/archive/fall2000/cs126/assignments/circuit.html

This is not my assignment though. I don't have the clock values. Thanks for helping me.

Thanks for your help. Can you please help me with the problem which I mentioned?

If I'm not mis-reading your write-up, the data in the file will all be read into your second array, each element in the array will correspond to one of the gates in the circuit, will have 1 or more inputs and 1 output. (For simulation purposes, you might want to include a fake gate with only an output, user-settable to hi or lo. And don't forget that once you start bundling gates into higher-order sub-circuits such as flip-flops and half-adders, you may also want more than one output, but your file-format doesn't allow for that yet)

Sounds like a data structure to me:

struct Gate {
    std::string gate_type;  // AND/OR/NOT/...
    std::vector<int> inputs;
    int output;  // or std::vector<int> outputs;
};

And then an array of Gate instances:

std::vector<Gate> gate_array;

Since you don't know a priori how many inputs a gate will have (but you can certainly error-check, and should, against the gate-type, a generic input function becomes:

std::ifstream& operator >>(std::ifstream & in, Gate & g)
{
    // get a line and prepare to read from it
    std::string line;
    if (! getline(in, line))
        return in;
    std::stringstream ins(line);

    // read a gate from the line
    ins >> g.gate_type;

    // to put the first N-1 integers into inputs, and the last one into output,
    // we need to know one step ahead of time when we get to the end of the line.
    // note: this will work for the case of no inputs (only an output) as well
    int a_val, next_val;
    ins >> a_val;
    while (ins >> next_val) {
        g.inputs.push_back(a_val);
        a_val = next_val;
    }
    g.output = a_val;
    return in;
}

Finally, read all the gates via:

Gate g;
while (myfile >> g)
    gate_array.push_back(g);

Your second array of all of the nets, you can create by looping over your gate_array, looking at the inputs and outputs, and making sure you have an entry for each one. I recommend using a std::map<> type for this, so you don't have to worry about what happens if a number gets skipped in the input. If the key-type is an integer, it acts pretty much exactly the same as an array, though you loop over the existing keys, rather than the number of items stored in the map. See cplusplus.com for more details, I find it to be an invaluable reference.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.