I want to read the matrix from file and store it in an array. But the array is storing only the last value of matrix. Can anyone explain this please?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
FILE *fp, *fp1;
int n = 0, i, j, a[4][4], b[16];
fp = fopen("Output.txt", "r");
if (fp == NULL) {
printf("\nError; Cannot open file");
exit(1);
}
while (!feof(fp)) {
i = 0;
fscanf(fp, "%d", &n);//reads the file containing matrix
b[i] = n;//this part is not working
printf("%d\n", n);
i++;
}
fclose(fp);
fp1 = fopen("Output2.txt", "w");
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
fprintf(fp1, "%d\t", a[i][j] * 2);
}
fprintf(fp1, "\n");//creates file of altered matrix
}
fclose(fp1);
return 0;
}
Your input loop is incorrect:
you reset i to 0 at the beginning of each iteration
you use an incorrect test: while (!feof(fp)). Learn here why: Why is “while ( !feof (file) )” always wrong? . You should instead test the array index against the array length and check if fscanf() succeeds at reading the next value.
Here is a corrected version:
for (i = 0; i < 16; i++) {
if (fscanf(fp,"%d",&n) != 1) { //reads the file containing matrix
fprintf(stderr, "invalid input\n");
exit(1);
}
b[i] = n;
printf("%d\n", n);
}
Note also that you do not read the values into the 2D matrix, so the output loop has undefined behavior because a is uninitialized.
Here is a improved version:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
int i, j, a[4][4];
fp = fopen("Output.txt", "r");
if (fp == NULL) {
fprintf(stderr, "Error: Cannot open file Output.txt for reading\n");
exit(1);
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if (fscanf(fp, "%d", &a[i][j]) != 1) {
fprintf(stderr, "invalid input for a[%d][%d]\n", i, j);
fclose(fp);
exit(1);
}
}
}
fclose(fp);
fp1 = fopen("Output2.txt", "w");
if (fp1 == NULL) {
fprintf(stderr, "Error: Cannot open file Output2.txt for writing\n");
exit(1);
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
fprintf(fp1, "%d\t", a[i][j] * 2);
}
fprintf(fp1, "\n");
}
fclose(fp1);
return 0;
}
Related
I am new to C programming and I am getting a THREAD 1: EXC_BAD_ACCESS(code = 1, address 0x68)
when I run my program. The purpose of my code is to read from a txt file that contains positive and negative numbers and do something with it.
#include <stdio.h>
int main (int argc, const char * argv[]) {
FILE *file = fopen("data.txt", "r");
int array[100];
int i = 0;
int num;
while( fscanf(file, "%d" , &num) == 1) { // I RECEIVE THE ERROR HERE
array[i] = num;
printf("%d", array[i]);
i++;
}
fclose(file);
for(int j = 0; j < sizeof(array); j++){
printf("%d", array[j]);
}
}
After
FILE *file = fopen("data.txt", "r");
Say
if(file == 0) {
perror("fopen");
exit(1);
}
Just a guess, the rest of the code looks ok, so likely this is the problem.
Also worth noting that you might have more than 100 numbers in your file, in which case you will blow past the size of your array. Try replacing the while loop with this code:
for (int i = 0; i < 100 && ( fscanf(file, "%d" , &num) == 1); ++i)
{
array[i] = num;
printf("%d", array[i]);
}
Do you have the file "data.txt" created and local?
touch data.txt
echo 111 222 333 444 555 > data.txt
Check that your file open succeeded.
Here is a working version,
#include <stdio.h>
#include <stdlib.h> //for exit
int main (int argc, const char * argv[])
{
FILE *fh; //reminder that you have a file handle, not a file name
if( ! (fh= fopen("data.txt", "r") ) )
{
printf("open %s failed\n", "data.txt"); exit(1);
}
int array[100];
int idx = 0; //never use 'i', too hard to find
int num;
while( fscanf(fh, "%d" , &num) == 1) { // I RECEIVE THE ERROR HERE
array[idx] = num;
printf("%d,", array[idx]);
idx++;
}
printf("\n");
fclose(fh);
//you only have idx numbers (0..idx-1)
int jdx;
for(jdx = 0; jdx<idx; jdx++)
{
printf("%d,", array[jdx]);
}
printf("\n");
}
I have some problems with my code. Everything is fine; gcc shows no errors or warnings; valgrind says no memory leaks, but my array isn't printed and I'm not sure why. Is fscanf() not working correctly or am I printing it incorrectly?
#include <stdio.h>
#include <stdlib.h>
int arrsize (FILE *file)
{
if (file == NULL) {
printf("Error: file pointer is null.");
return 0;
}
int sizer;
fscanf(file, "%d", &size);
return size;
}
int main()
{
int i = 0, j = 0, k = 0,a=0;
FILE *fp;
if ((fp = fopen("matrix.txt", "r")) == NULL)
{
printf("Error: file pointer is null.\n");
return 1;
}
int size = arrsize(fp);
printf("Array size is %d x %d \n", size, size);
double **array = (double **)malloc(size * sizeof(double *));
for (i; i < size; i++)
array[i] = (double *)malloc(size * sizeof(double));
for (i; i < size; i++)
{
for (j = 0; j < size; j++)
{
fscanf(fp, " %lf", &array[i][j]);
printf(" %lf ",array[i][j]);
}
}
for (k ; k < size; k++) {
free(array[k]);
}
free(array);
fclose(fp);
return 0;
}
Check the return of fscanf and malloc to make sure of success.
Add fflush ( stdout); to the print loops since the format string does not have a newline.
#include <stdio.h>
#include <stdlib.h>
int arrsize (FILE *file)
{
if (file == NULL) {
printf("Error: file pointer is null.");
return 0;
}
int size;
if ( 1 == fscanf(file, "%d", &size)) {
return size;
}
return -1;
}
int main()
{
int i = 0, j = 0, k = 0;
FILE *fp;
if ((fp = fopen("matrix.txt", "r")) == NULL)
{
printf("Error: file pointer is null.\n");
return 1;
}
int size = arrsize(fp);
if ( -1 == size) {
fprintf( stderr, "Error: problem parsing array size.\n");
return 1;
}
printf("Array size is %d x %d \n", size, size);
double **array = NULL;
if ( NULL != ( array = malloc(size * sizeof(double *)))) {
for (i = 0; i < size; i++) {
if ( NULL == ( array[i] = malloc(size * sizeof(double)))) {
fprintf ( stderr, "problem allocation array[]\n");
while ( i) {
i--;
free ( array[i]);
}
free ( array);
return 1;
}
}
}
else {
fprintf ( stderr, "problem allocation array\n");
return 1;
}
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
if ( 1 == fscanf(fp, "%lf", &array[i][j])) {
printf(" %lf ",array[i][j]);
}
else {
fprintf ( stderr, "problem parsing double\n");
break;
}
}
fflush ( stdout);
}
for (k = 0; k < size; k++) {
free(array[k]);
}
free(array);
fclose(fp);
return 0;
}
I am trying to do duplicate elimination from clients.txt (which has 7 names and surnames, some of them are repeated). In the end of file it writes the output to output.dat file. I did not get any error during the compiling but when i try to run it, it gives "003.exe stopped working" error. (003.c is C project name)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct names
{
char name[25];
char surname[25];
};
int main()
{
int i, j;
char a[1] = {""};
struct names name[200];
FILE *file;
FILE *file2;
file = fopen("clients.txt", "r");
if (ferror(file))
{
printf ("File could not be opened");
}
while (fscanf(file, "%s", a) == 2)
{
i = 0;
fscanf(file, "%s %s", name[i].name, name[i].surname);
i++;
}
for (i = 0; i < 200; i++)
{
for (j = 0; j < 200; j++)
{
if (i != j && strcmp(name[i].name, name[j].name) == 0 && strcmp(name[i].surname, name[j].surname) == 0 )
{
strcpy(name[j].name, a);
strcpy(name[j].surname, a);
}
}
}
fclose(file);
file2 = fopen("output.dat", "w");
{
for (i = 0; i < 200; i++)
{
if ( strcmp(name[i].name, "") == 1 )
{
fprintf(file, "%s %s\n", name[i].name, name[i].surname);
}
}
}
fclose(file2);
system("pause");
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct names {
char name[25];
char surname[25];
};
int isEqual(struct names *a, struct names *b){
return strcmp(a->name, b->name) == 0 && strcmp(a->surname, b->surname)==0;
}
int main(){
int i, j;
struct names name[200], a;
FILE *file;
file = fopen("clients.txt", "r");
if (!file){//ferror can't use to fopen
printf ("File could not be opened");
return -1;
}
i=0;
while (fscanf(file, "%24s %24s", a.name, a.surname) == 2){
int dup = 0;
for(j=0; j < i ;++j){
if(dup=isEqual(&a, &name[j]))
break;
}
if(!dup)//!dup && i<200
name[i++] = a;
}
fclose(file);
file = fopen("output.dat", "w");
for (j = 0; j < i; ++j){
fprintf(file, "%s %s\n", name[j].name, name[j].surname);
}
fclose(file);
system("pause");
return 0;
}
I have the following code:`
#include <stdio.h>
#include <stdlib.h>
void load_from_file(int A[], int *n);
int main()
{
int *A;
A = (int*)malloc(0);
int count = 0;
int i;
load_from_file(A, &count);
for(i = 0; i < count; i++)
{
printf("A[%d]=%d ", i, A[i]);
printf("\n");
printf("&A[%d]=%p \n\n", i, &A[i]);
}
return 0;
}
void load_from_file(int A[], int *n)
{
FILE* fp;
int temp, i;
fp = fopen("data.txt", "r");
if (fp == NULL)
{
printf("error!!");
exit (1);
}
fscanf(fp, "%d", &temp);
*n = temp;
A = (int*) realloc(A, temp * sizeof(int));
if (*A == NULL)
{
printf("error realloc!!");
exit(1);
}
for(i = 0; i < temp; i++)
{
fscanf(fp, "%d", &A[i]);
}
for(i = 0; i < temp; i++)
{
printf("A[%d]=%d ", i, A[i]);
printf("\n");
printf("&A[%d]=%p \n\n", i, &A[i]);
}
fclose(fp);
}
I'm trying to read a text file into an array.
First line of the file has the number of elements of the array, and second line the numbers-elements.
We create the array through realloc.
But something is going wrong.....
I have some patches, printing the address of array's elements.
But unfortunatelly they are different(not all the times) inside the function, and outside the function, although an array is passed by reference (as I think...)
Please, tell me where is the mistake, and how can I fix the problem.
Thanks in advance...
Dimitri
Call:
int *A = load_from_file(&count);
Function:
int *load_from_file(int *n)
{
FILE* fp;
int temp,i;
int *A = 0;
fp=fopen("data.txt","r");
if (fp==NULL){fprintf(stderr, "error!!\n");exit (1);}
fscanf(fp,"%d",&temp);
*n=temp;
A = (int*) realloc(A,temp * sizeof(int));
if (A == NULL) {fprintf(stderr, "error realloc!!\n");exit(1);}
for(i=0;i<temp;i++)
fscanf(fp, "%d",&A[i]);
for(i=0;i<temp;i++)
{
printf("A[%d]=%d ",i,A[i]);
printf("\n");
printf("&A[%d]=%p \n\n",i,&A[i]);
}
fclose(fp);
return A;
}
This is a totally minimal set of changes; the code needs a lot more work. In particular, the fscanf() calls should be error checked. There's a subtle but important change in the allocation test: if (A == NULL) rather than the original if (*A == NULL).
With somewhat more complete error checking:
int *load_from_file(int *n)
{
FILE *fp;
int temp, i;
int *A = 0;
const char file[] = "data.txt";
fp = fopen(file, "r");
if (fp == NULL)
{
fprintf(stderr, "Failed to open file %s for reading\n", file);
exit(1);
}
if (fscanf(fp, "%d", &temp) != 1)
{
fprintf(stderr, "Failed to read number of entries\n");
exit(1);
}
*n = temp;
A = (int *) realloc(A, temp * sizeof(int));
if (A == NULL)
{
fprintf(stderr, "Failed to reallocate %zu bytes of memory\n",
temp * sizeof(int));
exit(1);
}
for (i = 0; i < temp; i++)
{
if (fscanf(fp, "%d", &A[i]) != 1)
{
fprintf(stderr, "Failed to read entry number %d\n", i);
exit(1);
}
}
for (i = 0; i < temp; i++)
printf("A[%d]=%d: &A[%d]=%p\n", i, A[i], i, &A[i]);
fclose(fp);
return A;
}
I have a library of error reporting functions that I use which would reduce the error handling from 4 lines to 1 line per error. I strongly recommend creating and using such a library for yourself.
I am new to C programming and I am getting a THREAD 1: EXC_BAD_ACCESS(code = 1, address 0x68)
when I run my program. The purpose of my code is to read from a txt file that contains positive and negative numbers and do something with it.
#include <stdio.h>
int main (int argc, const char * argv[]) {
FILE *file = fopen("data.txt", "r");
int array[100];
int i = 0;
int num;
while( fscanf(file, "%d" , &num) == 1) { // I RECEIVE THE ERROR HERE
array[i] = num;
printf("%d", array[i]);
i++;
}
fclose(file);
for(int j = 0; j < sizeof(array); j++){
printf("%d", array[j]);
}
}
After
FILE *file = fopen("data.txt", "r");
Say
if(file == 0) {
perror("fopen");
exit(1);
}
Just a guess, the rest of the code looks ok, so likely this is the problem.
Also worth noting that you might have more than 100 numbers in your file, in which case you will blow past the size of your array. Try replacing the while loop with this code:
for (int i = 0; i < 100 && ( fscanf(file, "%d" , &num) == 1); ++i)
{
array[i] = num;
printf("%d", array[i]);
}
Do you have the file "data.txt" created and local?
touch data.txt
echo 111 222 333 444 555 > data.txt
Check that your file open succeeded.
Here is a working version,
#include <stdio.h>
#include <stdlib.h> //for exit
int main (int argc, const char * argv[])
{
FILE *fh; //reminder that you have a file handle, not a file name
if( ! (fh= fopen("data.txt", "r") ) )
{
printf("open %s failed\n", "data.txt"); exit(1);
}
int array[100];
int idx = 0; //never use 'i', too hard to find
int num;
while( fscanf(fh, "%d" , &num) == 1) { // I RECEIVE THE ERROR HERE
array[idx] = num;
printf("%d,", array[idx]);
idx++;
}
printf("\n");
fclose(fh);
//you only have idx numbers (0..idx-1)
int jdx;
for(jdx = 0; jdx<idx; jdx++)
{
printf("%d,", array[jdx]);
}
printf("\n");
}