Output 2 integer numbers separated by comma to array in C - 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]);
}

Related

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 !!

Displaying the output of a sorted array causes problem

I tried applying bubble sort algorithm to an array of values.
I have 5000 values in my input, from 1 to 5000. I imported values from a text file to create the array, which worked fine. The bubble sort algorithm also worked fine.
The issue is somewhere with the output. Some values do not appear in the output at all, whereas some values are being printed multiple times. I am attaching my code and the image of the output for reference.
#include<stdio.h>
#include<conio.h>
int main() {
FILE * fp;
long int i, j, n, temp;
printf("Enter array size:");
scanf("%ld",&n);
long int array[n];
fp = fopen("5000averagecase.txt", "r");
for (i=0; i < n; ++i)
fscanf(fp,"%ld",&array[i]);
for (i=0; i < n; ++i)
for (j=0; j < n-1-i; ++j)
if (array[j] > array[j+1]) {
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
temp = 0;
}
for (i=0; i< n; ++i)
printf("%ld\t", array[i]);
return(0);
}
https://i.stack.imgur.com/ZbxVU.png
Since your file has only values from 1 to 5000 and I assume they're not sorted in any way, why don't you just make another file like this
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// shuffles an array given a pointer to the array and its length
void shuffleArray(int *array, int len) {
srand(time(NULL));
int n1, n2, tmp;
for(int i = 1;i < len - 1; i++) {
// get random two positions in the array to swap
n1 = rand() % len, n2 = rand() % len;
// just swap'em
tmp = array[n1];
array[n1] = array[n2];
array[n2] = tmp;
}
}
int main() {
FILE * fp = fopen("5000averagecase.txt", "w");
// check for file pointer validity
if(fp == NULL) {
printf("Error: can't open the file!");
exit(0);
}
// creating an array of 5000 number
int arr[5000];
// fill it from 1 to 5000
for(int i = 0;i < 5000; i++) {
arr[i] = i + 1;
}
// shuffle it
shuffleArray(arr, 5000);
// write the array in `5000averagecase.txt` for later use
for(int i = 0;i < 5000; i++) {
fprintf(fp, "%d ", arr[i]);
}
// don't forget to close the file at the end
fclose(fp);
return 0;
}
Now after executing this you get your valid file, and after executing your sorting code on it you see that it works as expected

Updating matrix in c and overwriting it to a text file

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??

How to read integer numbers from file to an array?

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

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.

Resources