Updating matrix in c and overwriting it to a text file - c

Firstly I'am new to C language and this is a part of the project I'am doing as my minor. My main objective is to read a matrix from text file and store it in 2D array and then take input from user, the particular row and column for which the value is to be updated in matrix and then overwrite the previous matrix stored in text file.
My text file consists of matrix in the form:
0 0 1 2 3 4...
0 0 2 0 3 0...
0 2 7 5 3 1...
.
.
.
Here's my code:
#include<stdio.h>
#include <stdlib.h>
int main()
{
int i;
int j;
int mat[31][200];
FILE *file;
file=fopen("uid.txt", "r");
for(i = 0; i < 31; i++)
{
for(j = 0; j < 200; j++)
{
if (!fscanf(file, "%d", &mat[i][j]))
break;
}
}
fclose(file);
int col,value,row;
printf("enter row");
scanf("%d",&row);
printf("enter col");
scanf("%d",&col);
printf("enter changed value");
scanf("%d",&value);
mat[row][col]=value;
file=fopen("uid.txt", "w+");
for(i = 0; i < 31; i++)
{
for(j = 0; j < 200; j++)
{
fprintf(file,"%d ", mat[i][j]);
}
fprintf(file,"\n");
}
fclose(file);
}
But the values are not getting updated. Is there a way to update the matrix by using the same technique??

Related

How to store data in different array i c programming from a data file

Suppose I have the following two columns in a data file name data.txt
1 2
3 4
5 6
7 8
9 10
Now, I want to store the first column in one array and the second column in another array by using fscanf. Can someone tell me how to do it?
void main()
{
float a[20] b[20];
int i;
FILE* read = fopen("data.txt","r");
for (int i = 0; i < 10; i++) {
fscanf(read, "%f", &a[i]);
}
for(int i = 0; i < 10; i++) {
printf("%f\n", a[i]);
}
}
The above code does not do the job. Is there any way to do it? I want to store first column in a[] and second in b[].
You should scan first element to a[], then second element to b[]. And so on.
for (int i = 0; i < 10; i++) {
fscanf(read, "%f", &a[i]);
fscanf(read, "%f", &b[i]);
}

Passing an arg outputs file as null, program working if value passed during the execution of program

In a program for my CS class we're asked to read an array from a txt file and detect sequences of equal numbers in the rows or columns. I've done the program and it was working if it later on asks for the value that we store in "c" to see which number we want to look for in the array, but for some reason it does not work when I enter the value from the terminal (as an argument), outputting the file is NULL, when before it worked just fine.
This is the code for receiving the value as an argument:
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
#define square_location "square.txt"
int main(int argc, char * argv[]) {
int square[MAX][MAX], c;
if(argc!=2){
fprintf(stderr,"we're missing args!\n");
exit(1);
}
FILE *file;
file = fopen(square_location,"r");
if(file==NULL){
printf("Woops!\n");
exit(1);
}
sscanf(argv[1], "%d",&c);
int i=0,j=0;
for(i=0;i<MAX;i++) {
for(j=0;j<MAX;j++) {
fscanf(file, "%d", &square[i][j]);
}
}
printf("the file read is: \n");
for (int i = 0; i <MAX ; ++i) {
for (int j = 0; j < MAX; ++j) {
printf("%d ",square[i][j]);
}
printf("\n");
}
for (int i = 0; i <MAX ; ++i) {
for (int j = 0; j <MAX ; ++j) {
if(square[i][j]== c && square[i][j+1]==c && square[i][j+2]==c){
printf("sequence found at row %d !!\n",i+1);
}
}
}
for (int i = 0; i <MAX ; ++i) {
for (int j = 0; j <MAX ; ++j) {
if(square[i][j]== c && square[i+1][j]==c && square[i+2][j]==c){
printf("sequence found at column %d !!\n",j+1);
}
}
}
fclose(file);
return 0;
}
This is the code before any modifications:
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
#define square_location "square.txt"
int main(int argc, char * argv[]) {
int square[MAX][MAX],c;
FILE *file;
file = fopen(square_location,"r");
if(file==NULL){
printf("Woops!\n");
exit(1);
}
int i=0,j=0;
for(i=0;i<MAX;i++) {
for(j=0;j<MAX;j++) {
fscanf(file, "%d", &square[i][j]);
}
}
printf("the file read is: \n");
for (int i = 0; i <MAX ; ++i) {
for (int j = 0; j < MAX; ++j) {
printf("%d ",square[i][j]);
}
printf("\n");
}
printf("enter the value to find\n");
scanf("%d",&c);
for (int i = 0; i <MAX ; ++i) {
for (int j = 0; j <MAX ; ++j) {
if(square[i][j]== c && square[i][j+1]==c && square[i][j+2]==c){
printf("sequence found at row %d !!\n",i+1);
}
}
}
for (int i = 0; i <MAX ; ++i) {
for (int j = 0; j <MAX ; ++j) {
if(square[i][j]== c && square[i+1][j]==c && square[i+2][j]==c){
printf("sequence found at column %d !!\n",j+1);
}
}
}
fclose(file);
return 0;
}
Output:
the file read is:
1 1 1 3 4
1 3 2 4 1
1 2 2 2 0
0 2 2 0 0
0 0 0 0 0
enter the value to find
1
sequence found at row 1 !!

C array is overwriting an index

I am reading from 2 different files and running this function on each.
The file format is
2
3
8 7 6
5 4 3
The code I am using is
void readFile(char txtName[], int k, int matrix){
int c = 0;
int i = 0;
int j = 0;
int x = 0;
//Open file
FILE *txt;
txt = fopen(txtName, "r");
//Go till the end of the file
while( fscanf(txt, "%d", &c) != EOF ){
//For assigning matrix A
if(matrix == 1){
//Used to skip over the first 2 inputs.
if(x > 1){
//Got to the end of the row increase to the next row.
if(j == k){
i++;
j = 0;
}
//Assiging c to A
A[i][j] = c;
printf("%d A in matrix %d %d\n", A[i][j], i, j);
j++;
}
x++;
}
}
fclose(txt);
}
int main( int argc, char *argv[] ) {
readFile(argv[1], k, 1);
int m = 2;
int k = 3;
int i;
int j;
for(i=0; i < m; i++){
for(j=0; j < k; j++){
printf("%d", A[i][j]);
}
printf("\n");
}
}
I am trying to initialize an 2 dimensional array with the file so that it is a matrix. The first value of the file is the m and the second is the k. So from the file posted. it is a 2x3 matrix with the values below it. A is a global array set to A[2][3];
The issue I am having is when I run this code my array comes out with
8 7 5
5 4 3
and I have no clue why. I have put a lot of print statements for debugging. When it comes to i = 0 and j = 2 it prints out 6. But when I print the array after the function A[0][2] = 5. Where is the error that I am making?
Your problem probably with while( fscanf(txt, "%d", &c) != EOF ). Just try with while( fscanf(txt, "%d", &c)) and check.

Output 2 integer numbers separated by comma to array in C

I'm newbie C-er
What I want is to output the numbers from text file separated by comma, for example.
Input file:
1,2/3,4/5,6/7,8
Expected output is:
1,2
3,4
5,6
7,8
So it means 2 numbers separated by comma will be taken as a pair.
Below is my code which just gives 2 first number.
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *myFile;
myFile = fopen("somenumbers.txt", "r");
int numberArray[6];
int i;
for (i = 0; i < 6; i++)
{
fscanf(myFile, "%d,", &numberArray[i] );
}
for (i = 0; i < 6; i++)
{
printf("Number is: %d\n\n", numberArray[i]);
}
fclose(myFile);
return 0;
}
You can rewrite code like here (only conceptual piece of code).
int numberArray[6];
int i;
for (i = 0; i < 6; i+=2)
{
fscanf(myFile, "%d,%d/", &numberArray[i],&numberArray[i+1]);
}
for (i = 0; i < 6; i+=2)
{
printf("Number is: %d,%d\n\n", numberArray[i], numberArray[i+1]);
}

Writing values in a 2 dimensional array in c

Being new to C, and this website, I'm unfamiliar with this problem I'm having. I have a 2 dimensional array with [8][8] elements. I'm trying to get the user to enter numbers into the array until finished. The program is far from finished, but I'm stuck on this problem before I can move on. Basically I use a for loop to let the user enter into each element. However, when the first row is complete, it overwrites it's last value onto the first column second row element spot. How can I prevent this from happening: Here's my code:
#include <stdio.h>
#include <string.h>
int Check_rules();
void Print_Array(int array[][8], int size)
{
int i, j;
for (i = 0; i <= size; i++)
{
printf("\n");
for (j = 0; j <= size; j++)
{
printf("%d ",array[i][j]);
}
}
printf("\n\n");
}
int main()
{
int size = 8;
int i, j;
int fullArray[size][size];
int grid1[3][3];
int grid2[3][3];
int grid3[3][3];
int grid4[3][3];
int grid5[3][3];
int grid6[3][3];
int grid7[3][3];
int grid8[3][3];
int grid9[3][3];
for (i = 0; i <= size; i++)
{
for (j = 0; j <= size; j++)
fullArray[i][j] = 0;
}
printf("Want to play a game? Enter values 1-9 starting in row 1 column 1, \nand we will work our way from there. Here's the playing board.\nIt's Sudoku, so follow the rules of the game.\n\n");
for (i = 0; i <= size; i++)
{
printf("\n");
for (j = 0; j <= size; j++)
printf("%d ",fullArray[i][j]);
}
printf("\n\n");
int tmp;
char *keeper = (" ");//space for marker
for (i = 0; i <= size; i++)
{
for (j = 0; j <= size; j++)
{
printf("Enter first value(press 0 and ENTER to skip a box, \nand -1 to cancel game): ");
scanf("%d", &tmp);
if(tmp == -1)
return 0;
fullArray[i][j] = tmp;
Print_Array(fullArray,size);
}
}
return 0;
}
If you run this you'll see my problem when you enter the last value in row 1. It overwrites the second row first column element spot?
Everywhere you have <= size, you actually want < size. This is because C uses 0-based indexes. That means if you have an array with 5 elements, the indexes are 0, 1, 2, 3, 4. In a loop like for (int i = 0; i <= 5; i++), i would get the values 0, 1, 2, 3, 4, 5. That last one is an invalid index into the array. Using i < 5 fixes the problem (ensures i stops before it reaches 5).
Fixed and cleaned up version of your code:
#include <stdio.h>
#include <string.h>
void printArray(int size, int array[][size]) {
for (int i = 0; i < size; i++) {
printf("\n");
for (int j = 0; j < size; j++) {
printf("%d ", array[i][j]);
}
}
printf("\n\n");
}
int main() {
int size = 8;
int fullArray[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
fullArray[i][j] = 0;
}
}
printf("Enter values in row 1 column 1, and we will work our way from there. Here's the playing board. \n\n");
printArray(size, fullArray);
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
printf("Enter first value (press 0 and ENTER to skip a box, or -1 to cancel game): ");
int number;
scanf("%d", &number);
if(number == -1) {
return 0;
}
fullArray[i][j] = number;
printArray(size, fullArray);
}
}
return 0;
}
EDIT
To clarify, this is fixed version of the original code in the question. The new code is a bit different, but I think the issue is the same.

Resources