fscanf read file using c - c

My data.txt content is:
1 2 3 4 5 6
1 2 3 4 5 6
4 5 6 7 8 2
I read the file, and store the value to a two dimension int array
int record[line_number][6];
int record2[line_number][8];
int test;
for(i = 0; i <line_number; i++)
{
for(j = 0; j <6; j++)
{
fscanf(fptr, "%d", &record[i][j]);
}
}
int a=0;
int b=0;
for(a=0; a<i; a++) {
for(b=0; b<6; b++) {
printf("%d,", record[a][b]);
}
printf("\n");
}
The output like a memory address, what wrong in my code? Thanks!

You don't check the return value of fscanf(), so you don't know that it really succeeds for all the conversions. If it fails, the value in record[][] will be uninitialized, and printing it out will print whatever happens to be in memory.

Related

Segmentation fault: 11 in Counting Sort algorithm in C

I have written this code in C that implements the counting sort algorithm. I create an array of 10 elements and then I apply the counting sort. The code is this:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
int my_array[] = {10,10,9,9,6,5,4,3,2,1};
int i;
int N = 10;
//print the array
for (i=0; i<10; i++){
printf("%d\n", my_array[i]);
}
//define the minimum and the maximum as the first element of the array
int min_array=my_array[0];
int max_array=my_array[0];
printf("--------------\n");
//find the minimum and the maximum of the array
for(i=0; i<N; i++){
if(my_array[i]<min_array){
min_array=my_array[i];
}
else if(my_array[i]>max_array){
max_array=my_array[i];
}
}
//check if it worked
printf("max_array %d\n", max_array);
printf("min_array %d\n", min_array);
//
int range_array;
range_array = max_array - min_array +1;
int count_array[range_array+1];
for(i=0; i<range_array;i++)
count_array[i]=0;
int j=0;
for(int i=0; i<10; i++){
count_array[my_array[i]-min_array]=count_array[my_array[i]-min_array]+1;
}
int z = 0;
for(i=min_array; i<max_array; i++){
for(j=0; j<count_array[i-min_array];j++)
z=z+1;
my_array[z]=i;
}
for(i=0; i<N; i++){
printf("%d\n", my_array[i]);
}
}
When I execute it, I have Segmentation fault: 11
I do not know why, but I think it is related to some allocation problem, and in particular I think that the error comes from one of the last for loops before printing the ordered array. Indeed if I put some print check where it stops, the segmentation fault error comes at the end.
EDIT: I fixed count_array[range_array] into count_array[i] in the for loop. But now it seems not working properly:
The output is this:
10 10 9 9 6 5 4 3 2 1
--------------
max_array 10
min_array 1
--------------
1 2 3 4 5 6 9 9 2 1

Pointer pointing older address while printing the array elements through loop

I want to read array elements defined in main function through another user defined function. The array is 2D and it shows the first three elements correctly but as next loop starts the address pointed by that pointer is 2 step back from the intended address. Why's that?
Here is the main function calling the frame() function where the problem is:
void main(){
char dec,player[2][20];
int i,counter=0,palo,winner=0;
for(i=0;i<2;i++){
printf("Enter Player%d's name: ",(i+1));
scanf("%s",player[i]); //ASK PLAYER NAME
}
startAgain: //GAME RESTART POINT
system("cls");
palo=0;
char spot[][3]={"123","456","789"};
//------------------MAIN GAME AREA-------------------------------
for(counter=0;counter<9;counter++,palo++){
frame(*spot);
read(&palo,*spot,*player);
palo %=2;
}
}
Here is the frame() function:
void frame(char *count){
int i,j;
printf("\t\t\t");
line(24);
for (i = 0; i < 3; i++){
printf("\t\t\t");
for (j = 0; j < 3; j++){
printf("| %c ",(*(count+i)+j));
}
printf("|\n\t\t\t");
line(24);
}
}
The intended output is :
1 2 3
4 5 6
7 8 9
What it displays:
1 2 3
2 3 4
3 4 5
Make life easier for yourself and others, use normal array indexing instead of pointer arithmetic.
for(counter=0;counter<9;counter++,palo++){
frame(spot);
read(&palo,spot,player);
palo %=2;
}
...
void frame(char count[][3]){
int i,j;
printf("\t\t\t");
line(24);
for (i = 0; i < 3; i++){
printf("\t\t\t");
for (j = 0; j < 3; j++){
printf("| %c ",count[i][j]);
}
printf("|\n\t\t\t");
line(24);
}
}

Program using scanf in a loop prints incorrect output

I don't know what's happening with this code.
#include<stdio.h>
int main()
{
int ii[5], i;
for (i=1; i<=5; i++)
{
scanf("%d", &ii[i]);
}
printf("----------------------\n");
for(i=1; i<=5; i++)
printf("%d\n", ii[i]);
return 0;
}
After compiling when I provide input as
1 2 3 4 5
then it prints as it is,
but when I provide input in reverse order:
5 4 3 2 1
it keeps on asking up to some more digits and after that it prints out some random digits from given set of input.
How can I fix this?
c uses 0 indexing that means that array indexes start at 0 not 1. A for loop over an array should look like this:
int array[ARRAY_LENGTH], i;
for (i = 0; i < ARRAY_LENGTH; ++i) {
This will ensure that i will go from 0 to ARRAY_LENGTH - 1 and will not go outside the bounds of your array.
These lines:
for(i=1; i<=5; i++)
printf("%d\n", ii[i]);
will Access element 5 of ii where the maximum index is 4. This will cause Undefined Behavior which is likely why you are seeing random numbers appear.

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;
}

How to print from 1 to 9 with three numbers in a line using loops

I am coding a tic tac toe game in C. I am stuck at making a board like this:
1 2 3
4 5 6
7 8 9
I want to use loops so that I dont have to use a printf function with many \n's and \t's...
Here's my attempt:
for (i=0;i<=9;i++)
{
printf("\n\n\n\t\t\t");
for (j=i;j<=i+2;j++)
{
printf("%c\t",boarddots[j]);
}
if (i==3)
break;
}
Something like this, you could adapt it to your actual needs:
for(int i = 1; i <= 9; ++i)
{
printf("%d", i); // print numbers one by one
if (0 == i % 3)
printf("\n"); // start new line if current number is divisible by 3
}
P.S. Sorry for possible typos
for (int row = 0; row < 3; row++)
{
for (int column = 0; column < 3; column++)
{
printf("%d ", (row * 3) + column + 1);
}
printf ("\n");
}
/*
output:
1 2 3
4 5 6
7 8 9
*/
Your loop condition for (i=0;i<=9;i++) iterated once too many. Personally, I would uses a 2D array such as char board [3][3], but one step at a time to help with your immediate question.
#include<stdio.h>
char boarddots[] = "--O-XX-O-";
int main()
{
int i;
for (i=0; i<9; i++) {
if (i % 3 == 0)
printf("\n\n\n\t\t");
printf("\t%c",boarddots[i]);
}
return 0;
}
Could do print as a string and use string truncation:
char boarddots[9] = {'1','2','3','4','5','6','7','8','9'};
int loop;
for (loop=0; loop<9; loop+=3)
printf ("%.3s\n", &boarddots[loop]);
You don't need a NULL on the end of the char array as the truncation takes care of that.

Resources