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;
}
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 to read from a file which contains names and scores the results of a contest but the function fscanf is not working properly.. Does it have to do with newlines or something?
I'll leave below the code and the screenshots of the issue i'm having.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct contest
{
char name[25];
int score;
}Contest;
int main(int argc, const char * argv[]) {
Contest players[100];
FILE *fp;
fp = fopen("contest.txt", "r");
if(fp == NULL)
{
fprintf(stderr, "Error: failed to open the file\n");
exit(EXIT_FAILURE);
}
int cnt = 0;
while(fscanf(fp, "%s %d", players[cnt].name, &players[cnt].score) != EOF)
{
++cnt;
}
char temp[25];
for(int t = 0; t < cnt; ++t)
{
for(int u = 0; u < cnt; ++u)
{
if(strcasecmp(players[t].name, players[u].name) > 0)
{
strcpy(temp, players[t].name);
strcpy(players[t].name, players[u].name);
strcpy(players[u].name, temp);
}
}
}
for(int t = 0; t < cnt; ++t)
{
fprintf(stdout, "Name : %s Score : %d\n", players[t].name, players[t].score);
}
fclose(fp);
return 0;
}
Input:
Randal 34
Leonel 67
Vaughn 100
Missy 68
Cristopher 92
Dagmar 102
Blondell 88
Milly 83
Darrel 12
Josh 71
Output:
You've just made two little mistakes: (1) forgot to also swap the scores and (2) did the wrong comparison in the strcasecmp. For the rest, it's all working fine here.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
typedef struct contest
{
char name[32];
int score;
} Contest;
int main()
{
Contest players[100];
FILE *fp = fopen("contest.txt", "r");
assert(fp);
int cnt = 0;
while(fscanf(fp, "%s %d", players[cnt].name, &players[cnt].score) != EOF)
++cnt;
for(int i = 0; i < cnt; ++i)
{
for(int j = 0; j < cnt; ++j)
{
if(strcasecmp(players[i].name, players[j].name) < 0)
{
int tempScore = players[i].score;
players[i].score = players[j].score;
players[j].score = tempScore;
char tempName[32];
strcpy(tempName, players[i].name);
strcpy(players[i].name, players[j].name);
strcpy(players[j].name, tempName);
}
}
}
for(int i = 0; i < cnt; ++i)
fprintf(stdout, "Name : %s Score : %d\n", players[i].name, players[i].score);
fclose(fp);
return 0;
}
I have an Instance File from which I need to store the NUM_PT and all the respective co-ordinates in the form of a 2D array system (personal choice so I can access them easily). I am able to retrieve the NUM_PT but I am stuck at reading the successive cordinates into my array.
HERE IS WHAT I HAVE DONE
/* Assignment 2 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#define MAXS 256
int main(int argc, char *argv[])
{
int num_pt;
int inputfile = 0, outputfile = 0, i;
for (i = 1; i < argc; i++)
{
if (strcmp (argv[i], "-i") == 0)
inputfile = i+1;
if (strcmp (argv[i], "-o") == 0)
outputfile = i+1;
}
if (inputfile == 0)
{
/* invalid command line options */
printf("\nIncorrect command-line...\n");
printf("> %s [-i inputfile [-o outputfile]]\n\n", argv[0]);
exit(0);
}
FILE *fp;
fp = fopen(argv[inputfile], "r");
int count = 0;
if (fp == 0)
{
printf("\nCould not find %s\n", argv[inputfile]);
exit(0);
}
char line[MAXS];
while (fgets(line, sizeof line, fp) != NULL)
{
if (count == 4)
{
fscanf(fp, "%d", &num_pt);
break;
}
else
count++;
}
int arr[num_pt][1];
while (fgets(line, sizeof line, fp) != NULL)
{
if (count == 5)
{
int k, j, cord;
for (k = 0; k < num_pt; k++)
{
for (j = 0; j < num_pt; j++)
{
while (fscanf(fp, "%d%d", &cord) > 0)
{
arr[k][j] = cord;
j++;
}
}
}
}
}
fclose(fp)
return 0;
}
After retrieving NUM_PT i tried reinitializing the count to 5 because the cordinates start from **LINE 6* in the file.
ERROR FROM COMPILER
Language: c99 ; Compiler: gcc
sample for "Storing numbers as (x, y) cordinates from a file" (It is better not to fix the reading position)
#include <stdio.h>
typedef struct point {
int x, y;
} Point;
int readPoint(FILE *fp, Point *p);
int readInt(FILE *fp, int *n);
int main(void){
FILE *fp = fopen("instance10_001.txt", "r");
Point p;
int MAX_X, MAX_Y;
readPoint(fp, &p);
MAX_X = p.x;
MAX_Y = p.y;
printf("MAX_X:%d, MAX_Y:%d\n", MAX_X, MAX_Y);
int NUM_PT;
readInt(fp, &NUM_PT);
printf("NUM_PT:%d\n", NUM_PT);
Point arr[NUM_PT];
for(int i = 0; i < NUM_PT; ++i){
readPoint(fp, &arr[i]);
printf("Point(%d, %d)\n", arr[i].x, arr[i].y);
}
fclose(fp);
}
int readLine(FILE *fp, char *buff, int buff_size){
while(fgets(buff, buff_size, fp)){
if(*buff == '#' || *buff == '\n')
continue;
return 1;
}
return 0;
}
#define LINE_MAX 128
int readPoint(FILE *fp, Point *p){
char buff[LINE_MAX];
if(readLine(fp, buff, sizeof buff)){
return 2 == sscanf(buff, "%d %d", &p->x, &p->y);
}
return 0;
}
int readInt(FILE *fp, int *n){
char buff[LINE_MAX];
if(readLine(fp, buff, sizeof buff)){
return 1 == sscanf(buff, "%d", n);
}
return 0;
}
I have an input file a.txt:
1 abc 3
2 efgh 4.5
3 text 3
4 xyz 2
So basically, it has 3 columns, first one is int, second is text, and third is double. I need to read this file by rows (which actually works, I guess), but have some problems with writing only second and third column to another (b.txt) file. fprinft saves something like this:
0.000000
0.000000
0.000000
0.000000
xvæ$ 0.000000
instead of
abc 3
efgh 4.5
text 3
xyz 2
I simply need to save only the second and the third column from a.txt file to b.txt file. Here's my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct mypair
{
char string[1024];
double number;
} mypair;
void zero_string(char *string, int n)
{
int i;
for(i=0; i<n; i++)
string[i] = '\0';
}
int row(FILE* f, struct mypair *p)
{
int num;
if(!feof(f))
{
if(fscanf(f,"%d %s %lf", &num, p->string, &p->number) == 3)
{
return 0;
}
}
else
{
return 1;
}
}
int main(int argc, char **argv)
{
int n = 5, status = 0, i = 0, j;
struct mypair array[5];
char file_in_name[255];
char file_out_name[255];
FILE *fin;
FILE *fout;
zero_string(file_in_name, 255);
zero_string(file_out_name, 255);
printf("Data file:\n> ");
scanf("%s", file_in_name);
printf("Out file:\n> ");
scanf("%s", file_out_name);
fin = fopen(file_in_name, "r");
fout = fopen(file_out_name, "w");
if( fin == NULL )
{
exit(-1);
}
if( fout == NULL )
{
exit(-1);
}
while(status != 1)
{
status = row(fin, &array[i]);
i ++;
fprintf(fout, "%s %lf\n", array[i].string, array[i].number);
if(i >= n)
break;
}
fclose(fin);
fclose(fout);
for(j=0; j<i; j++)
printf("%s %lf\n", array[i].string, array[i].number);
return 0;
}
I modified the code, now it works, thanks!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct mypair
{
char string[1024];
double number;
} mypair;
void zero_string(char *string, int n)
{
int i;
for(i=0; i<n; i++)
string[i] = '\0';
}
int row(FILE* f, struct mypair *p)
{
int num;
if(!feof(f))
{
if(fscanf(f,"%d %s %lf", &num, p->string, &p->number) == 3)
{
return 0;
}
}
else
{
return 1;
}
}
int main(int argc, char **argv)
{
int n = 5, status = 0, i = 0, j;
struct mypair array[5];
char file_in_name[255];
char file_out_name[255];
FILE *fin;
FILE *fout;
zero_string(file_in_name, 255);
zero_string(file_out_name, 255);
printf("Data file:\n> ");
scanf("%s", file_in_name);
printf("Out file:\n> ");
scanf("%s", file_out_name);
fin = fopen(file_in_name, "r");
fout = fopen(file_out_name, "w");
if( fin == NULL )
{
exit(-1);
}
if( fout == NULL )
{
exit(-1);
}
printf("\n");
while(status != 1)
{
status = row(fin, &array[i]);
if(i >= n)
break;
else
{
if(status != -1)
fprintf(fout, "%s %lf\n", array[i].string, array[i].number);
}
i ++;
}
fclose(fin);
fclose(fout);
for(j=0; j<i; j++)
printf("%s %lf\n", array[j].string, array[j].number);
return 0;
}
In your bottom loop, you want to index your array by j, not i.
I've got a problem with my project. I don't know how to add an option which will save to file the result's from console. I think i shoud use fprintf but nothing works. Please help ;D
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct symbole
{
int symbol;
int czestosc;
};
struct symbole nowy[256];
void odczytIwyznaczenie(void);
void wyswietl(struct symbole nowy[]);
void sortuj(struct symbole nowy[]);
int main()
{
odczytIwyznaczenie();
sortuj(nowy);
wyswietl(nowy);
getchar();
return 0;
}
void odczytIwyznaczenie (void)
{
FILE *plik;
char n;
int i;
for (i=0; i<256; i++)
{
nowy[i].czestosc=0;
nowy[i].symbol=i;
}
plik = fopen("plik.txt","r");
if (plik == NULL)
{
printf("Blad odczytu!");
getchar();
exit(1);
}
while (n != EOF)
{
n = fgetc(plik);
for(i=0; i<256; i++)
{
if (nowy[i].symbol == n)
{
nowy[i].czestosc++;
break;
}
}
}
fclose (plik);
}
void sortuj(struct symbole nowy[])
{
int i, j, temp, temp1;
for (i = (256); i > 0; i--)
{
for (j = 1; j <= i; j++)
{
if (nowy[j-1].czestosc < nowy[j].czestosc)
{
temp = nowy[j-1].czestosc;
temp1=nowy[j-1].symbol;
nowy[j-1].czestosc = nowy[j].czestosc;
nowy[j-1].symbol=nowy[j].symbol;
nowy[j].czestosc = temp;
nowy[j].symbol=temp1;
}
}
}
}
void wyswietl(struct symbole nowy[])
{
int i;
for (i=0; i<256; i++)
{
if (nowy[i].czestosc!= 0)
printf ("%5d| %5c| %5d\n", nowy[i].symbol, nowy[i].symbol, nowy[i].czestosc);
}
}
You indeed need fprintf(), use it as follows
void wyswietl(struct symbole nowy[])
{
FILE *output;
int i;
output = fopen("OuptputFile.txt", "w");
if (output == NULL) /* maybe you don't have permission to create the file */
return;
for (i = 0 ; i < 256 ; i++)
{
if (nowy[i].czestosc == 0)
continue;
fprintf(output, "%5d| %5c| %5d\n", nowy[i].symbol,
nowy[i].symbol, nowy[i].czestosc);
}
}
Also, you have to initialize n before comparing it to EOF, change
while (n != EOF)
which is reached before n is initialized, to
while ((n = fgetc(pilk)) != EOF)