I just started leaning c programming, and i have been doing alright, but now am stuck with a problem, I do not know how to read information from a file in c. the file contains info like;
123456789123456789123456789123456789...and so on. i have to read the file in, and then group the numbers in the file into 7, make them a coulumn matrix and multipy each of the column matrix with another matrix. ive written the code to multiply two matrixs and it works, i just dont know how to group the numbers into groups of 7 and make them a colum matrix like below.
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9

Recommended Answers

All 15 Replies

Most of the time "I don't know" is not an attenuation for lack of effort in this forum.
If you don't know, try to lean, and prove at least, what effort you have done in such endeavor. Anything else would be an aggravation against your cause.

ive tried, i said ive got the last part of it working, just cant figure out the first part, which is read the file and turn it into column matrixes. actually when i read in the file i get the wrong info.

Post the relevant part of the code you have worked with.

>>123456789123456789123456789123456789

Does the file actually look like that -- just a bunch of numbers with no separation between them?

group the numbers in the file into 7

Into 7 what, seven columns per row, seven rows, seven digits per column, seven digits per row?

Show an example: Input, and what you need for your array. Your description leaves a lot to be desired. An example would serve much better.

yes the file contains numbers like that without spaces but they are binary numbers, for exam the file contains 1000101100010100101010010101....and so on. so am soppose to read in this file and group the numbers into 7. each 7 should then become a column matrix. using the numbers above ill have it like below.

1 1 0 0
0 0 0 0
0 0 1 1
0 0 0 0
1 1 1 1
0 0 0 0
1 1 1 1

i started the code like this

#include <stdio.h>

int main ()
{
      File *pFile;
      int name;
      pFile = fopen("prac.txt","r");
      fscanf(pFile, "%d",name);
      printf("The file contains %d\n);  // I did this part to know if it reads in correctly

      return 0;
}

I would call fgets() instead of scanf() to read the entire file into a character array so that they can be rearranged or displayed (printed) however you wish.

but if i read it in as a charcter file wont that prevent me from doing a matrix multiplication later on

"Each column should then become a column matrix." ??

Your description of the problem is quite poor. SHOW the input, SHOW the output you need and any special set up your problem requires. Do not rely on describing the problem. You suck at that, clearly. ;)

Forget your code, for now. Until we understand WHAT you're trying to do very clearly, your code is just a nuisance.

What AD is suggesting is this - get all your data into a buffer first, then it can be manipulated as necessary, from that point, step by step. What those steps should be, we still don't know.

ok what am actually trying to do is write an error correcting code, using the hamming(7,4) method. hope this decribes it better

i also updated the read in code

#include <stdio.h>

int main()
{
       FILE *pFile;
	char name[100];
if ((	pFile = fopen("prac.txt", "r")) == NULL)
	{
		printf("file could not be opend\n");
	}
	else
	{
	
	fgets(name, 100, pFile);
	}
	printf("it contains %s\n", name);	
	fclose(pFile);
	return 0;
}

now its reads the numbers as char but how do i get the numbers i read from the txt file to be interger values instead of char

Computers don't really do anything with char's - they're just numbers with a different value, so the computer can tell them apart. Those values vary depending on your systems character set, but most use values equal or close to, the ASCII set or table values. You should d/l one if you program.

In a typical character set, 0 is 48, and since they (the digits 0 through 9), are in sequence in the character set, the char digit, minus '0' (the char zero), equals the value of the number.

Here is how to convert a digit in the string to an integer. Just subtract '0' from the digit to get the int value. Now put that in a loop to assign each character of the string into a matrix.

char str[] = "1010101";
int digit0 = str[0] - '0';
int digit1 = str[1] - '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.