Reading two matrices from one txt file - c

I'm having trouble with one task.
I need to read two matrices from a text file and multiply them together.
The problem is that I know how to read one matrix but I don't understand how to read the two matrices separated (for example) by two spaces in a file.
Matrix.txt:
1 2 3 1 2 3
4 5 6 4 5 6
7 8 9 7 8 9
Maybe someone could explain me how to do that.
Now i'm trying to read first matrix:
#include <stdlib.h>
#include <stdio.h>
int main(){
FILE *fp;
int eSk = 0, sSk = 0,
matrix1[10][10] = {0}, i, j;
char c;
fp = fopen("matrix.txt", "r+");
while(!feof(fp)) {
while(getc(fp) != ' ') {
fscanf(fp, "%d", &matrix1[eSk][sSk]);
sSk++;
}
eSk++;
}
for(i = 0; i < eSk; i++) {
for(j = 0; j < sSk; j++) {
printf("%d ", matrix1[eSk][sSk]);
}
printf("\n");
}
puts("");
system("pause");
return 0;
}
what's wrong ?
Thank you for any help.

You need to skip the second matrix while reading the first and viceversa. Try this:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
FILE *file;
int matrixA[3][3], matrixB[3][3];
int garbage[3];
int i, j, k;
i = j = k = 0;
file = fopen("matrix.txt", "rt");
/* Read the first matrix */
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
fscanf(file, "%d", &matrixA[i][j]);
}
/* Skip the second matrix */
for (k = 0; k < 3; k++) fscanf(file, "%d", &garbage[k]);
}
/* Reposition the stream */
fseek(file, 0, SEEK_SET);
/* Read the second matrix */
for (i = 0; i < 3; i++) {
/* Skip the first matrix */
for (k = 0; k < 3; k++) fscanf(file, "%d", &garbage[k]);
for (j = 0; j < 3; j++) {
fscanf(file, "%d", &matrixB[i][j]);
}
}
/* Multiply them ... */
/* Show first matrix */
for (i = 0; i < 3; i++) {
printf("\n");
for (j = 0; j < 3; j++) {
printf(" %d ", matrixA[i][j]);
}
}
printf("\n");
/* Show second matrix */
for (i = 0; i < 3; i++) {
printf("\n");
for (j = 0; j < 3; j++) {
printf(" %d ", matrixB[i][j]);
}
}
return 0;
}

If the format of the string is standardized, you can read both matrices at one go. For your example above, please find a modified version as below.
int main(){
FILE *fp;
int eSk = 0, sSk = 0, i, j;
int mat1[3][3], mat2[3][3];
char line[256];
char c;
fp = fopen("matrix.txt", "r+");
if(NULL == fp) {
printf("Can't open the file\n");
exit(-1);
}
// Read one line
fgets(line, 256, fp);
while(!feof(fp)) {
// Convert line to array
sscanf(line,"%d %d %d %d %d %d", &mat1[eSk][0], &mat1[eSk][1], &mat1[eSk][2], &mat2[eSk][0], &mat2[eSk][1], &mat2[eSk][2]);
// Increment counter
eSk++;
//Read next line
fgets(line, 256, fp);
}
printf("Matrix1\n");
for(i = 0; i < eSk; i++) {
for(j = 0; j < 3; j++) {
printf("%d ", mat1[i][j]);
}
printf("\n");
}
printf("Matrix2\n");
for(i = 0; i < eSk; i++) {
for(j = 0; j < 3; j++) {
printf("%d ", mat2[i][j]);
}
printf("\n");
}
puts("");
system("pause");
return 0;
}

Related

How can I read a txt file to an array and sorting in ascending order?

I have a txt file includes 10000 passwords in it.I am trying to sort the passwords by length.Here is my function:
void bubbleSortASC(){
int n = 9999;
int i,j ;
char pw[n];
char temp;
FILE* fp;
fp = fopen("C:\\Users\\inanm\\Desktop\\project-work-2018555459\\10-million-password-list-top\\10000.txt", "r");
//fgets(pw, n , fp);
while(!feof(fp)){
fgets(pw, n , fp);
//printf("%s",pw);
}
for(i = 0; i < n-1;i++) {
for(j = i+1; j < n; j++){
if(strlen(pw[i]) > strlen(pw[j])){
strcpy(temp,pw[i]);
strcpy(pw[i],pw[j]);
strcpy(pw[j],temp);
}
}
}
fclose(fp);
printf("Ascending order of first 10 passwords are : \n");
for (i = 0; i < 10; i++){
printf("%s ", pw[i]);
}
printf("\n");
}
I've got no error but my output is empty.Can you help me to find the problem
This would do
void bubbleSortASC() {
const int lines = 9999; //< number of words in the file
const int max_width = 128; //< max width of a word
char pw[lines][max_width]; //< array of words
FILE* fp;
fp = fopen("words.txt", "r");
int curr = 0;
while (!feof(fp)) {
fgets(pw[curr++], max_width, fp);
}
fclose(fp);
char* sorted[lines]; //< array of char* will be in a sorted order
for (int i = 0; i < lines; i++) sorted[i] = pw[i];
// the bubble sort
for (int i = 0; i < lines - 1; i++) {
for (int j = 0; j < lines - i - 1; j++) {
if (strlen(sorted[j]) > strlen(sorted[j+1])) {
char* tmp = sorted[j];
sorted[j] = sorted[j+1];
sorted[j+1] = tmp;
}
}
}
printf("Ascending order of first 10 passwords are : \n");
for (int i = 0; i < 10; i++) {
printf("%s", sorted[i]);
}
printf("\n");
}

Why is my selection sort output different to my input?

So i have a file called output.txt which contains a list of numbers (12365 25460 12522 22707 8714 28771 235 11401 25150 26342 0) and i want to take them and pass them through my selection sort, ive managed to open the file and read them into my program but instead when the selction sort fiishes it comes out with a list of numbers that have nothing to do with my input (although they are in order as they should be)
#include <stdio.h>
int main() {
FILE *outp;
char arr[10];
outp = fopen("output.txt", "r");
if (outp == NULL)
{
puts("Issue in opening the input file");
}
while(1)
{
if(fgets(arr, 10, outp) ==NULL)
break;
else
printf("%s", arr);
}
fclose(outp);
int n=10;
int i, j, position, swap;
for (i = 0; i < (n - 1); i++) {
position = i;
for (j = i + 1; j < n; j++) {
if (arr[position] > arr[j])
position = j;
}
if (position != i) {
swap = arr[i];
arr[i] = arr[position];
arr[position] = swap;
}
}
for (i = 0; i < n; i++)
printf("%d\n", arr[i]);
return 0;
}
You're just reading the first 10 characters in the file, and setting the elements of arr to their character codes.
You need to parse the file contents as integers.
int arr[10];
for (i = 0; i < 10; i++) {
fscanf(outp, "%d", &arr[i]);
}

Is it possible to load a matrix and its size from a file?

Basically, the input file should look like this:
4
1 2 3 4
2 1 4 3
4 3 2 1
3 4 1 2
Where the first line is the size of the square matrix. However, I can't properly load the input directly into n and then into the matrix. I avoided such problem by creating a "loader" array.
int n, loader[100], p=0;
while(feof(data) == 0) {
if(p == 0) {
fscanf(data, "%d", &n); //taking n
p++;
} else {
fscanf(data, "%d", &loader[p]); //taking matrix values for p>0
p++;
}
}
//loading the matrix
int mat[n][n], o = 1; //o is set to 1, as the loader
//has been loaded from position 1.
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++){
printf("%d\n", loader[o]);
mat[i][j] = loader[o];
o++;
}
}
However, I think it is just unnecessary and there might be a faster way to directly take the matrix size and its values. When I programmed in such way, I received a welcoming Segmentation Fault error.
This code seems to work:
int loadMatrix(char *pth)
{
int i, j, n, **mat, p = 0, num;
FILE *fd;
fd = fopen(pth, "r");
if(fd == NULL) return(-1);
/* Read matrix size */
if(fscanf(fd, "%d", &n) == EOF) return(-1);
printf("Size: %d\n", n);
mat = malloc(n * sizeof(int *));
for(i = 0; i < n; i++) mat[i] = malloc(sizeof(int) * n);
while(fscanf(fd, "%d", &num) != EOF && p < (n * n)) {
mat[p / n][p % n] = num;
p++;
}
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
for(i = 0; i < n; i++) free(mat[i]);
free(mat);
fclose(fd);
return(0);
}

image convolution from ppm file

Good afternoon to everyone.
Firstly, i would like to apologize for my code - i am a real beginner in C. My problem is - i am given ppm file and i would need to store values from there into an array. I have already stored height , width and max value of color, now my idea to store a values would be the as shown at the picture - multiplying by three because it is in R G B format.
thank you for your help and please concider the fact i am a real beginner in C.
my code and output
#include <stdio.h>
int main(int argc, char** argv) {
int i = 0;
int j = 0;
FILE *fp;
fp = fopen(argv[1], "r");
printf(" %s ", argv[1]);
printf("\n");
int firstLine[2];
int width;
int next;
int enter;
int loop;
int height;
int max_color;
int pix[width][height];
int mask[3][3] = {// inicializting our given mask
{0, -1, 0},
{-1, 5, -1},
{0, -1, 0}
};
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", mask[i][j]);
}
printf("\n");
}
fscanf(fp, "%s", &firstLine);
fscanf(fp, "%d", &height);
fscanf(fp, "%d", &width);
fscanf(fp, "%d", &max_color);
printf("%p", firstLine);
printf("\n");
printf("%d ", width);
printf("\n");
printf("%d", height);
printf("\n");
printf("%d", max_color);
printf("\n");
for (i = 0; i < width * 3; i++) {
for (j = 0; j < height * 3; j++) {
loop = fscanf(fp, "%d", &enter);
pix[i][j] = enter;
printf("%d ", enter);
}
}
// fclose(fp);
return (EXIT_SUCCESS);
}
You cannot define array with unknown size, esp height and width are not initialized. You should use dynamic allocated array here, like this:
int ***pix;
pix = malloc(height * sizeof(int**));
for (i = 0; i < height; i++) {
pix[i] = malloc(width * sizeof(int**));
for (j = 0; j < width; j++) {
pix[i][j] = malloc(3 * sizeof(int));
}
}
To correct parse the binary, you cannot use formatted input because they are for strings. You can use fread instead, e.g. to read the width:
fread(&width, sizeof(int), 1, fp);
Then to fill this array:
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
for (k = 0; k < 3; k++) {
fread(&enter, sizeof(int), 1, fp);
pix[i][j][k] = enter;
}
}
}
This only works if what you said about ppm file's format is correct of course.

Can't read properly with fscanf

I have the following difficulties. I'm trying to read this file.
3
1 2 3
2
4 5
The numbers that are alone are the size of the array (3 and 2). And the following numbers are the array.
So 3 is the size of (1,2,3) and 2 is the size of (4,5).
I wrote a code on C to read that numbers and store them in arrays that use malloc().
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main(){
int i=0, j, *size, *vector;
size=(int*)malloc(sizeof(int));
vector=(int*)malloc(sizeof(int));
FILE *file;
file=fopen("file.dat", "rt");
if (file==NULL){
printf("Exit ...");
exit(1);
}
else {
do{
fscanf(file,"%d",&size[i]);
for(j=0;j<=size[i];j++){
fscanf(file,"%d",&vector[j]);
}
i++;
}while(feof(file)==0);
}
fclose(file);
return 0;
}
Is reading the file properly but if I printf the numbers I get:
3
5
2
3
4
2
Two things to mention here.
You have allocated memory for only one variable through malloc(), but you try to access beyond the allocated memory in the do...while loop. You need to realloc(), as and when required.
Please see Why is “while ( !feof (file) )” always wrong?
#include <stdio.h>
#include <stdlib.h>
int main(void){
int i, j, size, *size_v, **vector;
FILE *file;
file=fopen("file.dat", "rt");
if (file==NULL){
printf("Exit ...");
exit(1);
}
i = 0;//scan file
while(EOF != fscanf(file, "%d", &size)){
for(j = 0; j < size; j++){
fscanf(file, "%*d");
}
i++;
}
rewind(file);
size_v = malloc(i * sizeof(int));
vector = malloc(i * sizeof(int*));
i = 0;//read file
while(EOF != fscanf(file, "%d", &size_v[i])){
vector[i] = malloc(size_v[i] * sizeof(int));
for(j = 0; j < size_v[i]; j++){
fscanf(file, "%d", &vector[i][j]);
}
i++;
}
fclose(file);
//print & deallocate
size = i;
for(i = 0; i < size; ++i){
printf("%d\n", size_v[i]);
for(j = 0; j < size_v[i]; ++j){
printf("%d ", vector[i][j]);
}
printf("\n");
free(vector[i]);
}
free(vector);
free(size_v);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(void){
int i, j, size, *vector;
FILE *file;
file=fopen("file.dat", "rt");
if (file==NULL){
printf("Exit ...");
exit(1);
}
i = 0;
while(EOF != fscanf(file, "%d", &size)){
printf("%d\n", size);//each loop
vector = malloc(size * sizeof(int));
for(j = 0; j < size; j++){
fscanf(file, "%d", &vector[j]);
}
for(j = 0; j < size; j++){
printf("%d ", vector[j]);
}
printf("\n");
free(vector);
i++;
}
fclose(file);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(void){
int i, j, k, size, size_sum, *size_v, *vector;
FILE *file;
file=fopen("file.dat", "rt");
if (file==NULL){
printf("Exit ...");
exit(1);
}
i = 0;//scan file
size_sum = 0;
while(EOF != fscanf(file, "%d", &size)){
size_sum += size;
for(j = 0; j < size; j++){
fscanf(file, "%*d");
}
i++;
}
rewind(file);
size_v = malloc(i * sizeof(int));
vector = malloc(size_sum * sizeof(int));
i = 0;//read file
k = 0;
while(EOF != fscanf(file, "%d", &size_v[i])){
for(j = 0; j < size_v[i]; j++){
fscanf(file, "%d", &vector[k++]);
}
i++;
}
fclose(file);
//print & deallocate
size = i;
size_sum = 0;
for(i = 0; i < size; ++i){
printf("%d\n", size_v[i]);
for(j = 0; j < size_v[i]; ++j){
printf("%d ", vector[j + size_sum]);
}
printf("\n");
size_sum += size_v[i];
}
free(vector);
free(size_v);
return 0;
}

Resources