fscanf into a 2, 2dArrays from a text file - c

Im being asked to:
Write a program that takes as input two 3x3 arrays in file "Matrices.txt", whose contents are:
3 7 1 3 4 3 6 5 0
4 1 5 2 0 4 6 1 2
I am unsure how I am to go about using fscanf to pull the numbers from the txt file and put them into two seperate 2d arrays.
This is what I got going on:
`
#include <stdio.h>
#include <stdlib.h>
int main(){
int matrixA [3][3];
int matrixB [3][3];
FILE *fptr;
int i,j = 0;
fptr = fopen("Matrices.txt","r");
if(fptr == NULL){
printf("Errpr! File was not created!");
exit(1);
}
while(!feof(fptr)){
fscanf(fptr,"%d%d", &matrixA[i][j]);
i++;
j++;
}//while loop
fclose(fptr);
return 0;
}
`

Related

Scanning numbers from a file into a two dimentional array

I am new to programming so I apologize if I mess up on anything.
So I am trying to scan a file with multiple lines of numbers and put them into a two dimensional array. I've tried looking at other questions relating to this and nothing has worked so far.
So I have tried using nested loops to scan the array and put the numbers inside but nothing seems to happen.
Inside the txt file is as follows:
0 7 9 4 0 0 8 0 4 5 0 1
0 2 4 0 0 0 1 6 2 8 6 0
0 1 1 1 1 0 8 5 6 8 0 7
0 5 1 0 0 0 1 3 8 1 0 1
Every 12th number is a new line.
#include <stdio.h>
#define BUF_SIZE 12
#define ROW 4
#define COL 12
void
barcodeArray(int barcode[ROW][COL])
{
char buffer[BUF_SIZE];
FILE* f = fopen("q5_input.txt","r");
if(f == NULL)
{
printf("no such file.");
}
for(int k = 0; k < ROW; k++)
{
for(int j = 0; j < COL; j++)
{
fscanf(f, "%1d", &barcode[k][j]);
printf("%ls", &barcode[k][j]);
}
}
fclose(f);
}
int
main(void)
{
int barcode[ROW][COL];
barcodeArray;
}
The printf inside the for loops is just reading back the numbers as it inputs the numbers in the array. As the code is it compiles but does nothing.
You must call your function with argument barcodeArray(barcode);
Edit : If you are not sure of the size of the array you can take a look at dynamically allocated variables. This is an important part of C programming
Try this way. I think using freopen() is easier and hassle free. It enables you to use same I/O functions that you use for console I/O operations.
#include <stdio.h>
#define BUF_SIZE 12
#define ROW 4
#define COL 12
void barcodeArray()
{
int barcode[ROW][COL];//This can be declared inside the function.
char buffer[BUF_SIZE];
FILE* f=freopen("q5_input.txt","r",stdin);
if(f == NULL)
{
printf("no such file.\n");
return;
}
for(int k = 0; k < ROW; k++)
{
for(int j = 0; j < COL; j++)
{
scanf("%d",&barcode[k][j]);
printf("%d ",barcode[k][j]);
}
printf("\n");
}
fclose(f);
}
int main(void)
{
barcodeArray();
}
Additionally if you want to output it in a file you can do the following in the main function:
int main(void)
{
freopen("out.txt","w",stdout);
barcodeArray();
}

C - Mixed Text File Data > To Different Arrays (Int and String)

I have just an ordinary "project.txt" file with this data:
Programming 10 3 4 5 4 3 2 4 5 2 3
Mathematics 8 3 3 4 5 3 2 2 3
Physics 6 3 4 5 3 4 5
Design 6 5 4 5 3 2 4
Logistics 8 3 4 5 3 1 1 2 3
Need to open this file, read and write all this data to arrays.
I need to somehow divide String and Integers from each other.
* NO NEED FOR IT RIGHT NOW, but later I will need to write text and numbers to different files. ***NO NEED TO DO THIS NOW*
Just need to do it with 2 different int and char arrays, but I am sitting for few hours and can't find normal explanation of how to divide this string from other things.
Here is my code, can anybody help me?
#include <stdio.h>
#include <stdlib.h>
int main(void) {
FILE *fp;
fp = fopen("C:\\Project\\project.txt", "r");
char arrayWords[140];
int i;
if(fp == NULL){
printf("Can't Read The File!");
exit(0);
}
for (i = 0; i < 140; i++){
fscanf(fp, "%s,", &arrayWords[i]);
}
for (i = 0; i < 140; i++){
printf("Number is: %s\n\n", &arrayWords[i]);
}
fclose(fp);
return 0;
}
This is the output I get, it is really confusing (Some part of it)...
Number is: P13454324523M833453223P6345345D6545324L834531123
Number is: 13454324523M833453223P6345345D6545324L834531123
Number is: 3454324523M833453223P6345345D6545324L834531123
Number is: 454324523M833453223P6345345D6545324L834531123
Number is: 54324523M833453223P6345345D6545324L834531123
Number is: 4324523M833453223P6345345D6545324L834531123
Number is: 324523M833453223P6345345D6545324L834531123
Number is: 24523M833453223P6345345D6545324L834531123
Number is: 4523M833453223P6345345D6545324L834531123
Number is: 523M833453223P6345345D6545324L834531123
Number is: 23M833453223P6345345D6545324L834531123
Number is: 3M833453223P6345345D6545324L834531123
Number is: M833453223P6345345D6545324L834531123
Number is: 833453223P6345345D6545324L834531123
Number is: 33453223P6345345D6545324L834531123
Number is: 3453223P6345345D6545324L834531123
Number is: 453223P6345345D6545324L834531123
Number is: 53223P6345345D6545324L834531123
Number is: 3223P6345345D6545324L834531123
Number is: 223P6345345D6545324L834531123
Number is: 23P6345345D6545324L834531123
Number is: 3P6345345D6545324L834531123
Number is: P6345345D6545324L834531123
Number is: 6345345D6545324L834531123
Number is: 345345D6545324L834531123
Number is: 45345D6545324L834531123
Number is: 5345D6545324L834531123
Number is: 345D6545324L834531123
Number is: 45D6545324L834531123
Number is: 5D6545324L834531123
Think problem is in pointers, but don't know, I am kind of a beginner and can't find any problems.
Thanks to everyone
It's easy to parse these lists of space-separated numbers using the strtol() function. This is a good solution because it handily sets a pointer to the next block of whitespace/non-number after the number it just converted. If there's no number to convert, it just sends back the original pointer. So the code tests for this to know when it's complete.
EDIT: Updated to handle name too.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *number_str = "SomeWords 19 9 6 2 22 1 0 -4";
char *ptr;
char *next_number;
int numbers[1000];
int number_count = 0;
char name[100];
next_number = number_str;
// Copy off the name
ptr = strchr(number_str, ' ');
if (ptr != NULL)
{
strncpy(name, number_str, ptr-number_str); // TODO - check bounds of name[]
name[ptr-number_str] = '\0';
// Point to the first number
next_number = ptr+1;
printf("Stored name [%s]\n", name);
}
// Then parse all the numbers
do
{
ptr = next_number;
long num = strtol(ptr, &next_number, 10);
if (ptr != next_number) // found one
{
numbers[number_count] = (int)num;
printf("Stored %3d into numbers[%d]\n", numbers[number_count], number_count);
number_count += 1;
}
} while(ptr != next_number);
return 0;
}
Outputs:
Stored name [SomeWords]
Stored 19 into numbers[0]
Stored 9 into numbers[1]
Stored 6 into numbers[2]
Stored 2 into numbers[3]
Stored 22 into numbers[4]
Stored 1 into numbers[5]
Stored 0 into numbers[6]
Stored -4 into numbers[7]

For loop and Array output confusion. Why is the middle section of this array not the same as the beginning and the end?

I run through this on paper, and I can't figure out why when list[i]=list[3], makes the output 4,0 and not 2,2. Where am I tracing this wrong? Numbers after that don't stack up as I think they should.
The first three and the last three outputs make sense. But the middle 4, I don't understand how they are getting [4,0], [1,2], [2,1], [0,4]
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
int list[10]= {2,1,2,4,1,2,0,2,1,2};
int line[10];
for(i=0; i<10;i++)
line[i]= list[9-i];
for(i=0; i<10; i++)
printf("%d %d\n", list[i], line[i]);
return 0;
}
output:
2 2
1 1
2 2
4 0
1 2
2 1
0 4
2 2
1 1
2 2

How to scanning lines, each line differently, in c?

Lets say i have this text file for example:
4
1 2 3 4
3 9 8 7
1 1 2 1
8 7 8 6
I want to store the first line ("4") to one variable, and the other lines,
insert them to 2d matrix as the way they showing (dynamic 2d array).
Notice that its just example, i just know that the first line is one char, and i don't know the len of the rest of the lines except that are N*N matrix.
How can i do this in C?
Edited: so the matrix should only have numbers, so sor example this txt file:
4
1 2 3 4
3 9 8 7
1 W 2 1
8 7 8 6 is illegal . how can i handle this?
#include <stdio.h>
#include <stdlib.h>
int main(void){
FILE *fp = fopen("data.txt", "r");
int n;
fscanf(fp, "%d", &n);
int (*mat)[n] = malloc(sizeof(int[n][n]));
int *p = &mat[0][0];
while(p < &mat[n-1][n])
fscanf(fp, "%d", p++);
fclose(fp);
//check
for(int r=0; r < n; ++r){
for(int c=0; c < n; ++c)
printf("%d ", mat[r][c]);
printf("\n");
}
free(mat);
return 0;
}

C code reading a file including two columns (numbers) and print them with array

I have a question about c code reading two columns in a file and print them.
I have a a bond.txt file including two columns like,
1 2
1 3
1 25
1 6682
4 5
4 6
4 13117
7 21
7 24
I code the following below.
#include<stdio.h>
#define ROW 10
#define COL 1
int readresults(FILE *results, FILE *fout, int score[][COL]);
int main()
{
FILE *f=fopen("bond.txt", "r");
FILE *fout=fopen("out.txt", "w");
int score[ROW][COL];
readresults(f, fout, score);
printf("score[5][1] = %d\n", score[5][1]);
}
readresults(FILE *results, FILE *fout, int score[][COL])
{
int row, col, item, i, j;
for(row=0; row<ROW; row++)
{
col = 0;
item = fscanf(results, "%d%d", &score[row][col], &score[row][col+1]);
fprintf(fout, "%d %d", score[row][col], score[row][col+1]);
printf("[%d %d]=%d [%d %d]=%d\n", row, col, score[row][col], row, col+1, score[row][col+1]);
fprintf(fout, "\n");
}
return 0;
}
When I looked at the output file. The items have no problem. However, the print in main function is different.
For example, in output file, score[5][1] = 6, but in main function, score[5][1] = 4 on my screen.
Set the COL constant to 2
Your matrix have 2 columns, so you must define it with COL = 2

Resources