How to read integer numbers from file to an array? - c

An error always occurs just like (stream != null)
What does it mean?
I shoul write a program then reads numbers to an array that prints them out. Otherwise everything will be bad(
#include <stdio.h>
#define M 4
int main() {
int i = 0, b = 0;
int myArray[M];
FILE *myFile;
myFile = fopen("D:\file1.txt", "rt");
for (i = 0; i < M; i++) {
fscanf(myFile, "%d", myArray[i]);
}
fclose(myFile);
for (i = 0; i < M; i++) {
printf("%d", &myArray[i]);
}
return 0;
}

Related

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

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

Files and generating numbers ,c programming

Task is next:
After you generate 20 random numbers and read informations from file tombula.txt which contains names of users and their 9 numbers,print the first user (his/her name) who has 3 numbers from own array equal to generated numbers.
This is my part of code where i generate numbers and read from file but I don't know how to find that first person who has 3 numbers from array equal to generated numbers,if you know how to write that please help:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
typedef struct
{ char name[50];
int num[8];
}someone;
void generating(int[]);
int check(int[], int);
int main(void)
{
someone *pK = NULL;
int i, j, count = 0, tip;
FILE *F;
int generated[30];
int brojac[5];
srand(time(NULL));
generating(generated);
for (int j = 0; j < 20; j++)
printf("%d ", generated[j]);
printf("\n\n\n");
F = fopen("tombula.txt", "r");
if (F == NULL)
{
printf("error!");
exit(1);
}
i = 0;
while (feof(F) == 0)
{
pK = ((someone *)realloc(pK, (i + 1)*sizeof(someone)));
fscanf(F, "%s", pK[i].name);
for (j = 0; j < 8; j++)
fscanf(F, "%d", &pK[i].num[j]);
i++;
count++;
}
i--;
count--;
for (int z = 0; z < count; z++){
printf("%s ", pK[z].name);
for (int k = 0; k < 8; k++){
printf("%d ", pK[z].num[k]);
}
printf("\n");
}
return 0;
}
void generating(int numbers[])
{
int i, tmp;
for (i = 0; i < 20; ++i)
{
tmp = rand() % 50 + 1;
if (check(numbers, tmp))
{
--i;
continue;
}
numbers[i] = tmp;
}
}
int check(int b[], int a){
int i;
int n = 8;
for (i = 0; i < 20; ++i)
{
if (b[i] == a)
return 1;
}
return 0;
}
1.You opened file in r mode.
2.In a loop (loop must work until EOF) read data stored in file using fscanf() .Store name into char array and number into integer array (As you defined in struct).
3.Inside loop check if the 3 numbers matches with the numbers generated and if they do print the persons name and use break to come out of loop.
4.After this close the file.

(C) Radix Sort array from text file

I am attempting to get this queue-based radix sort to work, but I can't seem to figure out what's wrong with it. It uses a text file as the input medium and throws tons of errors when I try to compile it and run it with the text file.
Any advice would be helpful at this point.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 10
#define SHOWPASS
//Compiled Using GNU GCC Compiler
void radixsort(int *a[], int n)
{
int i, b[MAX], m = *a[0], exp = 1;
for (i = 0; i < n; i++)
{
if (*a[i] > m)
m = a[i];
}
while (m / exp > 0)
{
int queue[10] =
{ 0 };
for (i = 0; i < n; i++)
queue[*a[i] / exp % 10]++;
for (i = 1; i < 10; i++)
queue[i] += queue[i - 1];
for (i = n - 1; i >= 0; i--)
b[--queue[*a[i] / exp % 10]] = *a[i];
for (i = 0; i < n; i++)
*a[i] = b[i];
exp *= 10;
#ifdef SHOWPASS
printf("\nPASS : ");
radixsort(a, n);
#endif
}
}
int main( int argc, char *argv[] )
{
if ( argc != 3 )
{
printf("Need two input parameters in the following order: \n 1. Input file path \n 2. Number of elements in file\n");
return 0;
}
int num_elements = atoi(argv[2]);
int *input_arr = (int*) calloc (num_elements, sizeof(int));
int i;
FILE *fin; //File pointer to read input file
fin = fopen(argv[1], "r"); //Initialize file pointer
for(i=0; i<num_elements; i++)
{
fscanf(fin, "%d", &(input_arr[0]));
}
radixsort(input_arr[0], i);
printf ( "\nArray before sorting: \n") ;
for ( i = 0 ; i < num_elements ; i++ )
printf ( "%d\t", input_arr[0] ) ;
printf ( "\n\n");
return 0;enter code here
}
There are plenty of errors in your code. Firstly, the way you are taking input is incorrect.
fscanf(fin, "%d", &(input_arr[0]));
While taking input, the array input_arr is filled with a single input value at input_arr[0]. The rest of the input is over-written at input_arr[0].
Replace it with, fscanf(fin, "%d", &(input_arr[i]));.
Even you are displaying output in incorrect way.After sorting, the same output will be displayed num_elements times because of the following incorrect statement:
printf ( "%d\t", input_arr[0] ) ;
Again replace the above statement with printf ( "%d\t", input_arr[i] ).
As an impact of following incorrect statement,
fscanf(fin, "%d", &(input_arr[i])); ,
your program experiences a Segmentation fault, since in function radixsort, you are iterating from 0 to n-1(Number of elements).
for (i = 0; i < n; i++)
{
if (*a[i] > m)
m = a[i];
}
As only a[0] is filled with input value and rest of the array(from a[1] to a[n-1]) contains a garbage value, you will get a runtime error while executing your code.
There are lot of other bugs too, which i had fixed. This is the perfect running code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 10
// #define SHOWPASS
// Compiled Using GNU GCC Compiler
void radixsort(int a[], int n)
{
int i, b[MAX], m = a[0], exp = 1;
for (i = 0; i < n; i++)
{
if (a[i] > m)
m = a[i];
}
while (m / exp > 0)
{
int queue[10] = { 0 };
for (i = 0; i < n; i++)
queue[a[i] / exp % 10]++;
for (i = 1; i < 10; i++)
queue[i] += queue[i - 1];
for (i = n - 1; i >= 0; i--)
b[--queue[a[i] / exp % 10]] = a[i];
for (i = 0; i < n; i++)
a[i] = b[i];
exp *= 10;
#ifdef SHOWPASS
printf("\nPASS : ");
radixsort(a, n);
#endif
}
}
int main(int argc, char *argv[])
{
if (argc != 3)
{
printf
("Need two input parameters in the following order: \n 1. Input file path \n 2. Number of elements in file\n");
return 0;
}
int num_elements = atoi(argv[2]);
int *input_arr = (int *)calloc(num_elements, sizeof(int));
int i;
FILE *fin; // File pointer to read input file
fin = fopen(argv[1], "r"); // Initialize file pointer
for (i = 0; i < num_elements; i++)
{
fscanf(fin, "%d", &(input_arr[i]));
}
radixsort(input_arr, i);
printf("\nArray before sorting: \n");
for (i = 0; i < num_elements; i++)
printf("%d\t", input_arr[i]);
printf("\n\n");
return 0;
}

Reading two matrices from one txt file

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

Resources