Jump to content

#include <iostream>
#include <fstream>

using namespace std;

int main() {
    const int maxHam = 12;
    const int maxBin = 8;
    int ham[maxHam] = { 0 }, gray[maxBin] = { 0 }, binary[maxBin] = { 0 };
    int pbit1 = 0, pbit2 = 0, pbit3 = 0, pbit4 = 0, p1cnt, p2cnt, p3cnt, p4cnt;
    ifstream myIn;
    myIn.open("packets.dat");
    while (true) {  //this is true for testing purposes
        p1cnt = 0;  //set our counters to 0
        p2cnt = 0;
        p3cnt = 0;
        p4cnt = 0;
        for (int i = 0; i < 12; ++i) { //read in the line into an array
            myIn >> ham[ i ];
        }
        pbit1 = ham[0]; //determining parity bits
        pbit2 = ham[1];
        pbit3 = ham[3];
        pbit3 = ham[7];
        //check for errors when recieving
        for (int i = 0; i < 12; i + 2) {
            if (ham[ i ] == 1) {
                p1cnt++;
            }
        }
        for (int i = 1; i < 12; ++i) {
            if (i == 1 || i == 2 || i == 5 || i == 6 || i == 9 || i == 10) {
                if (ham [ i ]== 1)
                    p2cnt++;
            }
        }
    }
}

 

I am trying to read from a long file of 0's and 1's and this current code will only read in 0's (or isnt reading anything at all) whats wrong with this? Also what should i put in the while to make it read until the end of file? 

 

Example File:

000011110111

000010101010

110010111000

110010111000

010001110010

110011111101

Link to comment
https://linustechtips.com/topic/775112-troubles-reading-from-file/
Share on other sites

Link to post
Share on other sites

1 minute ago, HeaterUp said:

oh wow completely missed that. Thanks

You're also reading in an int which will end up reading the entire line an possibly overflowing. You should be reading in character by character and converting each char to an int (assuming I get what you're trying to do.)

1474412270.2748842

Link to post
Share on other sites

7 minutes ago, fizzlesticks said:

You're also reading in an int which will end up reading the entire line an possibly overflowing. You should be reading in character by character and converting each char to an int (assuming I get what you're trying to do.)

So i created a new char input;

and changed it to

myIn>>input;

ham[ i ] = int(input);

this is setting all the values now to -52 not to sure where this is coming from? 

Link to post
Share on other sites

27 minutes ago, HeaterUp said:

So i created a new char input;

and changed it to

myIn>>input;

ham[ i ] = int(input);

this is setting all the values now to -52 not to sure where this is coming from? 

You can't just cast from char to int. A char of '0' has a value of 48, not 0. If 0 and 1 are the only things in the file you can do something like

ham[ i ] = (input == '0') ? 0 : 1;

I have no idea how you're getting -52, try printing out the input as you read it in to see if you're reading things in correctly.

1474412270.2748842

Link to post
Share on other sites

10 minutes ago, fizzlesticks said:

You can't just cast from char to int. A char of '0' has a value of 48, not 0. If 0 and 1 are the only things in the file you can do something like

ham = (input == '0') 0 : 1;

 

I have no idea how you're getting -52, try printing out the input as you read it in to see if you're reading things in correctly.

Output: cout << input <<endl; cout << ham[ i ] <<endl; 


-52

-52

-52

-52

-52

-52

-52

-52

-52

-52

-52

does this mean that im reading incorrectly from the file?

Link to post
Share on other sites

13 minutes ago, fizzlesticks said:

You can't just cast from char to int. A char of '0' has a value of 48, not 0. If 0 and 1 are the only things in the file you can do something like

ham = (input == '0') 0 : 1;

 

I have no idea how you're getting -52, try printing out the input as you read it in to see if you're reading things in correctly.

Sorry to waste your time, added an assert(myIn) and found the problem was with where my file was located. 

But reading to the end of the file would doing while (myIn>>input) work? (considering I take into account that the first char will need to be added before the for loop?)

Link to post
Share on other sites

8 minutes ago, HeaterUp said:

Sorry to waste your time, added an assert(myIn) and found the problem was with where my file was located. 

But reading to the end of the file would doing while (myIn>>input) work? (considering I take into account that the first char will need to be added before the for loop?)

That should work, yes.

1474412270.2748842

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×