Hello,

I want to use Hamming Code to correct any BER's that I have streaming from a text file.
The contents of my text file are as follows:
0 0 1 0 1 1 0 0 0 1 0 1 1 1 1 1 1 0 0 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 0 0 1 0 1 1 0 .

I want to split them up into 6 different 7x1 vectors so that each element in my vector (e.g. c2i1[7][1]) contains the corresponding element from the text file.

Ultimately, the 7x1 vector will be multiplied by 3x7 Hamming parity matrix (e.g. G[3][7]) and use the mod-2 (%2) to find where the error is, if there is any. After the BER's are corrected I want to translate it into ASCII and display the word.

This is what I have so far:

#include <stdio.h>
#include "conio.h"
#include <string.h>
#include <math.h>

int main()
{
	FILE *pFile;
	int c2i1[7][1], c2i2[7][1], c2i3[7][1], c2i4[100][1], c2i5[100][1], c2i6[100][1]; //char to int arrays
	int c, i=0, j=7, k=14, l=21, m=28, n=35; //counters
	int p1[3][1], p2[3][1], p3[3][1], p4[3][1], p5[3][1], p6[3][1]; //Parity check Matrix 


	/*Hamming Generator Matrix*/
	int G[3][7] = {{1,0,1,0,1,0,1}, {0,1,1,0,0,1,1}, {0,0,0,1,1,1,}};

	/*Opens file*/
	pFile = fopen("data_streamF.txt", "r");
	
	/*Prints stream on screen*/
	/*printf("This is the incoming stream:\n");
	c = fgetc(pFile);
	while (c != EOF)
	{
		printf("%c", c);
		c = fgetc(pFile);
	}
	printf("\n");
	
	/*Prints the first 7 bits*/
	printf("These are the first 7 bits:\n");
	while ( !feof (pFile) && i<7 )
	{	
		fscanf(pFile, "%d", &c2i1[i][0]);
		printf("%d ", c2i1[i][0]);
		i++;
	}
	
	printf("\n");

	printf("These are the second 7 bits:\n");
	/*Prints the second 7 bits*/
	while (!feof (pFile) && j<14)
	{
		fscanf(pFile, "%d", &c2i2[j-7][0]);
		printf("%d ", c2i2[j-7][0]);
		j++;
	}

	printf("\n");

	/*Prints the third 7 bits*/
	printf("These are the third 7 bits:\n");
	while (!feof (pFile) && k<21)
	{
		fscanf(pFile, "%d", &c2i3[k-14][0]);
		printf("%d ", c2i3[k-14][0]);
		k++;
	}

	printf("\n");

	/*Prints the fourth 7 bits*/
	printf("These are the fourth 7 bits:\n");
	while (!feof (pFile) && l<28)
	{
		fscanf(pFile, "%d", &c2i4[l-21][0]);
		printf("%d ", c2i4[l-21][0]);
		l++;
	}

	printf("\n");

	/*Prints the fifth 7 bits*/
	printf("These are the fifth 7 bits:\n");
	while (!feof (pFile) && m<35)
	{
		fscanf(pFile, "%d", &c2i5[m-28][0]);
		printf("%d ", c2i5[m-28][0]);
		m++;
	}

	printf("\n");

	/*Prints the fifth 7 bits*/
	printf("These are the sixth 7 bits:\n");
	while (!feof (pFile) && n<42)
	{
		fscanf(pFile, "%d", &c2i6[n][0]);
		printf("%d ", c2i6[n][0]);
		n++;
	}

	printf("\n\n");

	
	/*for(i=0; i<7; i++)
	{
		for (j=0;  j<2 ; j++)
		{
			p1[j][0] = (c2i1[i][0]*G[0][i]);
				
				if (p1[j][0 ]%2 == 0)
				printf("No bit error");
		}
	}
	*/

	fclose(pFile);
	
_getch();
return 0;
}

Welcome to the forum, Kgal! ;)

Your last row in G[][] is missing a digit.

So you want to change the name of the array the data is going into, in the outer loop of the input?

for(each array) { //change array names with every loop
   for(each element to be scanned into this one array) {
   
   }
}

Is that what you want?

Using 1 row per vector, with 7 columns doesn't appeal to you?

I have just understood that i cannot take in an ASCII character straight into an an array, but i have to make it a string containing the characters of the file, then convert them into and "int" type 7x1 vector.
What i meant was that I wanted for example that element x in string[x] will be the first character from the file i open, and loop that until EOF.

I'd recommend strongly that you re-configure the arrays for c2i. Use one array, set up like this:

int c2i[6][7]; //or whatever your sizes should be.

Now instead of hassling with different array names, you have it all together in one array, and can refer to each subarray through the index: c2i[0], or c2i[1], or c2i[2], etc. And use the columns for each part of the data, rather than the rows.

Say you had a string buffer, with your data, taken from a row of text in a text file:

char buffer[128]; //plenty of extra room is good here
FILE *fp;

fgets(buffer, 128, fp); //gets your data into a char string.

//Now we need to sscanf() using a for loop to put it into the right int array:
for(i=0;i<6;i++) {
   sscanf(buffer, etc., c2i[i]);
}

I'm not sure of the particulars of the format for this, but sscanf() is just like scanf(), except the source of the data, is only a string, instead of keyboard or a file.

I'll post back with the answer to this, in a bit.

You have read in a row of text from the file, with the code, into buffer (I used an array to avoid the file, for this example.)

This moves it from the buffer, as a string, into the int c2i[6][7] array.

#include <stdio.h>

int main() { //
   int i,j,k,ok,n;
   int c2i[6][7];
   
   char buffer[128]={"0 0 1 0 1 1 0 0 0 1 0 1 1 1 1 1 1 0 0 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 0 0 1 0 1 1 0\n"};
   k=ok=0;
   for(i=0;i<6;i++) {
      for(j=0;j<7;j++) {
         ok=sscanf(buffer+k, "%d",&n);
         if(ok) {
            c2i[i][j] = n; 
            //printf("i: %d  k: %d, %d",i, k,c2i[i][j]); getchar();
            k+=2;
         }
      
      }
   }
   printf(" %s\n",buffer);
   for(i=0;i<6;i++)
      for(j=0;j<7;j++) 
         printf("%2d",c2i[i][j]);

   printf("\n");
   return 0;
}
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.