I need help creating a program. I have a text file that is 0's and 1's which create a picture. There are 33 rows and 50 columns. Every row contains 0's and 1's. what i need to do is do a program that will read the zeroes and ones and display it as follows: 2 4 5 2 9, this on my text file would show 0011110000011000000000. This is what i have so far but it is not working

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

int main()
{
  string filenameIn="house.txt";
  string filenameOut="casa.txt";
  const int elements = 1650;
  int i, house[elements], count = 0;
  double avg;
  ifstream inFile;
  ofstream outFile;
  
  inFile.open(filenameIn.c_str());
  outFile.open(filenameOut.c_str());
  
  if (inFile.fail())
  {
  	cout<<"\nThe file was not successfully opened."
		<<"\nPlease check that the file currently exist."<<endl;
	exit(1);
   }
   
   if (outFile.fail())
   {
   	cout<<"The out file was not successfully opened."<<endl;
	exit(1);
   }
  inFile>>house[elements];
 	  
  if(inFile.good())
  { 
  for (i = 0; i < elements; i++)
  
  	if(house[i]!=house[i+1])
		outFile<<house[i];
		count++;
  
   }
  return 0;
}

Dani AI

Generated

The goal is run-length encoding each 50-column row of the 0/1 image (example: "0011110000011000000000" -> "2 4 5 2 9"). The line inFile >> house[elements] in the original post writes into a single, out-of-bounds index and does not read the file; was correct to suggest a loop. A safer, clearer approach is to read the file line-by-line (use std::getline) and count consecutive identical characters per line. That keeps rows separate, avoids off-by-one access (e.g., house[i+1] at the array end) and makes validation straightforward.

A compact, robust implementation pattern:

#include <fstream>
#include <string>
#include <vector>

std::ifstream in("house.txt");
std::ofstream out("casa.txt");
std::string line;
while (std::getline(in, line)) {
    if (!line.empty() && line.back() == '\r') line.pop_back();
    if (line.empty()) continue;
    std::vector<int> runs;
    char prev = line[0];
    int cnt = 1;
    for (size_t i = 1; i < line.size(); ++i) {
        char c = line[i];
        if (c != '0' && c != '1') continue;
        if (c == prev) ++cnt; else { runs.push_back(cnt); cnt = 1; prev = c; }
    }
    runs.push_back(cnt);
    for (size_t i = 0; i < runs.size(); ++i) {
        if (i) out << ' ';
        out << runs[i];
    }
    out << '\n';
}

Troubleshooting notes and gotchas: check that files actually open; strip Windows CR ('\r') from lines; validate each line has 50 characters (or handle shorter/longer lines explicitly); avoid accessing array[i+1] when i is the last index; always use braces for if/for blocks to prevent accidental logic errors (the original count++ placement looks affected by missing braces). If the input is a single continuous stream of 1650 bits (no newlines), read the whole file into one string and process it in 50-character slices instead of getline. These changes resolve the garbage output and make the program robust.

Recommended Answers

All 5 Replies

I don't understand what you're trying to do in the following line:

inFile>>house[elements];

But if you're trying to read all the data from the file into the array using that one line then it's wrong. You need to use a loop to enter the data into each element of house (from house[0] to house[end]) one number at a time.

so can i just take:

inFile>>house[elements];
out of the program? As for the for loop can you help me with that? Am i going to have another for loop since i have one already or is that incorrect?thanks for your post

Well, you want to have each element of the array store a single number, right? So you'll have to have at least one loop to go through the file and place the data into the array. You can use the for loop if your data is always the same length or a while loop if you have variable length data.

So, if you don't know how large the data section is, but that it will be less than some number... say 5000, then you can do something like this:

int main()
{
    ifstream inFile;
    inFile.open("data.txt");
    int data[5000];
    int current = 0;

    while (inFile >> data[current] && current < 5000)
    {
        cout << data[current] << endl;
        current++;
    }

    return 0;
}

Now, if you want you can add all your extra code within the while loop (or for loop... whatever you choose) and do your comparisons, write to the output file or whatever you want. So really, you only need one loop but you can do it in 2 loops if you really want.

My main point was that if you do something like:

inputFile >> data[100]

Then you're placing one single digit into one single location, location 100 to be precise. You're not putting the entire file into the array. If you're not convinced then try to print your array from your original code and you'll see that you get a bunch of garbage.

Well i know that there will always be 1650 elements in the array. What would i need to write to let the program read from the beginning to the end of the file. Yes i do see what you are talking about with the inFile part. When i run the program it does give me a bunch of garbage. How would i need to place the inFile part so that it will read all of the text file?

Well, you know that we're assigning one value from your file to your array via: inFile >> data. So, as long as you put it in any loop then you're done. You can do it in a for loop if you want.


for (int i = 0; i < END; i++)
{
      inFile >> data[i];
     //Do some comparisons, output etc here
}

That's all there is to it.

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.