I get a bad access error in the middle of the for loop, always when i=4. Does anybody know the reason for this? It works until i=4, but I don't see why I wouldn't get the bad access error in any other part of the for loop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXF 51
#define MAXFILE 200
int recommend(int fid, char *funcs[]){
int i;
for(i=0; i<fid; i++)
*funcs++;
printf("\nRecommended Function: %s\n", *funcs);
return 0;
}
int overlap(char *list[], char name[], int n){
int over=0, fid=202, i, j, k, m;
for(i=0; i<n; i++){
m=strlen(*list);
int lap=0;
for(j=0; j<(strlen(name)-1); j++){
for(k=0; k<m; k++)
if(list[i][k]==name[j]){
lap+=1;
break;
}
}
if(over<lap){
over=lap;
fid=i;
}
*list++;
}
return fid;
}
int readfile(char *flist[], FILE *fptr){
char a[MAXF];
int size=0;
while(fscanf(fptr, "%s\n", a) != EOF){
flist[size]=malloc(sizeof(char)*(1+strlen(a)));
strcpy(flist[size++],a);
}
return size;
}
int main () {
int n, id;
char fnname[MAXF], filename[MAXF], *flist[MAXFILE];
FILE *fp;
printf("Name of network file: ");
gets(filename);
printf("\nFunction Name: ");
gets(fnname);
fp=fopen(filename, "r");
if(fp==NULL)
printf("\nCould not open file.\n");
else {
n=readfile(flist, fp);
id=overlap(flist, fnname, n);
recommend(id, flist);
}
return 0;
}
It looks to me as if this:
m=strlen(*list);
should be:
m=strlen(list[i]);
And this:
*list++;
should not be there at all.
Related
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define KEYSIZE 16
int main(){
FILE *fptr;
fptr = fopen("program.txt", "w");
if (fptr == NULL) {
printf("Error!");
exit(1);
}
int num_keys = 1524006529 - 1523999329;
const char *keys[num_keys];
int idx = 0;
int j;
for (j = 1523999329; j<= 1524006529; j++){
int i;
char key[KEYSIZE];
srand (j);
for (i = 0; i< KEYSIZE; i++){
key[i] = rand()%256;
printf("%.2x", (unsigned char)key[i]);
fprintf(fptr, "%.2x", (unsigned char)key[i]);
}
fprintf(fptr, "%s", "\n");
printf("\n");
keys[idx] = key;
idx ++;
}
fclose(fptr);
return 0;
}
I want to create a text file (program.txt) that has a new string key variable on each line. The code above produces a txt file that has this content though:
h?G??+?
Mp???+??
a?G????#*Jb`e0?H?????G??????
S6&}?Μ??G??OFf??|???s8f?G????ɼ???5q??(???K?G?? ??J????9?+???G?????cZ?n0?Jr?G?????#O9]??????jH??G???krT?̇)?A
?g?v?G??????h^(?^?
û/?G???^vB?~W?39???G??\????"?iJ_????G??p??>??????I???U?G????????:??o?RV???G??/?C?^?????|?۬?G??q#0.L$+??Nv?????G????u? L?Ϩ????dv?G??[??C「<??i???G??UyM??Ƭ/??CxXUě?G?????φ
:W&?G????=J???o???I??G???G%DA+"?~5?Z¡?G??øL#??O=e?b?u.O?G????*jjɥ+T?)?օ?G??2w }a,ȃ??tC?s?G??J"/c??Mn???f?y??G????p?Ay?c????~"<?G?????Ӆ?=%??#???G??o?\??<w???4WϚBE?G??}oh??(??#HK???&?G????Vfz
???e?s?G??/?Ye??iV?؛?,-te?G??I?Ҁx??5?B!b?4?G??6Z??9?P?GcB?}???Y?~|"??dd??G??????=?ms?
??G?????^??[H??\:???G??2(J??A??????G???????o#?i?/???G??P?5?5?5X??
What am I doing wrong here?
This isn't all of the program, but it doesn't run past this function. It seems to have the segfault near the fscanf statement, but I don't think that's what's causing the error. I really don't see anything that could cause the segfault error, but I'm an amateur and probably missed something. All of the printf statements are for debugging purposes.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define N 10
struct point2d_def
{
int x;
int y;
};
typedef struct point2d_def point2d;
void fill(char str[], point2d P[])
{
FILE *ifp = NULL;
ifp = fopen(str, "r");
int i = 0;
if (ifp == NULL)
{
printf("Could not open %s for reading\n", ifp);
exit(1);
}
for(i = 0; i < N; ++i)
{
fscanf(ifp, "%d %d", &P[i].x, &P[i].y);
}
fclose(ifp);
printf("Fill function passed\n");
return;
printf("Blorp");
}
first you are declaring P[N] inside the func, and receiving P in the parameters.
second, you are writing to 11 slots instead of 10:
for(i = 0; i < N; ++i){
fscanf(ifp, "%d %d", &P[i].x, &P[i].y);
}
remove the =...
Programm crushes, help me please, thanks in advance. The file can be downloaded below.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int deed_id;
char deed_name[55];
int points_for_deed;
int total_times_done;
int total_points_earned;
} deed;
int main(){
FILE *file1;
file1=fopen("deed_list.txt", "r");
if(file1==NULL){
printf("Can not open the file");
return 1;
}
int j;
fscanf(file1, "%i", &j);
deed **deed_list = (deed**)malloc(sizeof(deed)*j);
int i;
for(i=0; i<j; i++){
fscanf(file1, "%i %s %i", &deed_list[i]->deed_id, deed_list[i]->deed_name, &deed_list[i]->points_for_deed);
}
printf("%i",j);
fclose(file1);
return 0;
}
https://sst-csci.com/csci151/wp-content/uploads/deed_list.txt
deed **deed_list = (deed**)malloc(sizeof(deed)*j);
You don't want a pointer to pointer to store an array of deed, change to a single pointer (and don't cast malloc):
deed *deed_list = malloc(sizeof(deed)*j);
Don't forget to call free(deed_list); at the end.
I'm trying to store the .txt file into struct, not being successful.
I dont need the first line stored, but I do need the rest.
Thanks guys !! =)
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int main(int argc, const char * argv[]) {
// insert code here...
printf("Hello, World!\n");
struct candidates
{
char name[25];
char gender[5];
int height;
int weight;
};
struct candidates values[25];
FILE *fp;
if ((fp=fopen("candidatesdata.txt","r"))==NULL){printf("Unable to open file\n");}
int x = 0;
int iterations = 0;
while((fscanf(fp,"%s,%s,%d,%d",&values[x].name, &values[x].gender, &values[x].height, &values[x].weight))!=EOF)
{
x++;
iterations +=1;
}
fclose(fp);
//values[15].weight = 300;
printf("\n%d\t%d",iterations,values[1].height);
return 0;
}
Text file looks like the following:
Name,Gender,Height,Weight
Tanner,M,71.8,180.25
John,M,70.75,185.3
Parker,F,65.25,120.3
Meeks,M,57.25,210.2
struct candidates
{
char name[25];
char gender[5]; // can be char gender too !!
int height; // should be changed to float height
int weight; // should be changed to float height
};
If you actually have :
Name,Gender,Height,Weight
in your file, you might do his before you go to the while loop :
char somebigstring[100];
.
.
.
if(fscanf(fp,"%s",somebigstring) == EOF) //wasting the first line
exit(-1); // exiting here if there is no first line
fix like this:
#include <stdio.h>
#include <stdlib.h>
struct candidates{
char name[25];
char gender[7];//Female+1
float height;
float weight;
};
int main(void) {
struct candidates values[25];
FILE *fp;
if ((fp=fopen("candidatesdata.txt","r"))==NULL){
fprintf(stderr, "Unable to open file\n");
exit(EXIT_FAILURE);
}
char line[64];
int x = 0;
int iterations = 0;
while(fgets(line, sizeof line, fp)){
iterations += 1;
if(x < 25 && sscanf(line, "%24[^,],%6[^,],%f,%f", values[x].name, values[x].gender, &values[x].height, &values[x].weight)==4){
x++;
} else if(iterations != 1){
fprintf(stderr, "invalid data in %d line: %s", iterations, line);
}
}
fclose(fp);
for(int i = 0; i < x; ++i){
printf("%s,%c,%.2f,%.2f\n", values[i].name, *values[i].gender, values[i].height, values[i].weight);
}
return 0;
}
This code does two following things; first is writes data into .txt file and the second it reads data from .txt file and stores in struct data type which is what you are trying achieve but only for single entity. Next you should implement this as for entities.
#include<stdio.h>
#include<stdlib.h>
typedef struct{
char name[30];
//other fields
}Candidate;
typedef struct{
Candidate c;
char city[30];
int vote;
}CityVotes;
//for .bin operations
void write_to_file(FILE *f)
{
Candidate c;
CityVotes cv;
printf("Enter candidate name : ");
scanf("%s",&c.name);
cv.c = c;
printf("Enter the city : ");
scanf("%s",&cv.city);
printf("Enter the vote : ");
scanf("%d",&cv.vote);
fwrite(&cv, sizeof(cv), 1, f);
}
//for .txt operations
void write_to_file1(FILE *f)
{
Candidate c;
CityVotes cv;
printf("Enter candidate name : ");
scanf("%s",c.name);
cv.c = c;
printf("Enter the city : ");
scanf("%s",&cv.city);
printf("Enter the vote : ");
scanf("%d",&cv.vote);
fprintf(f,"%s ", cv.c.name);
fprintf(f,"%s ", cv.city);
fprintf(f,"%d ", cv.vote);
}
//for .bin operations
void read_file(FILE *f)
{
CityVotes cv;
while(fread(&cv, sizeof(cv), 1, f)){
printf("Candidate : %s\t",cv.c.name);
printf("City.: %s\t",cv.city);
printf("Vote.: %d\n",cv.vote);
}
}
//for .txt operations
void read_file1(FILE *f){
CityVotes cv;
fscanf(f," %s", &cv.c.name);
fscanf(f," %s", &cv.city);
fscanf(f," %d", &cv.vote);
printf("Candidate : %s\t",cv.c.name);
printf("City.: %s\t",cv.city);
printf("Vote.: %d\n",cv.vote);
}
int main(void)
{
FILE *f;
f=fopen("candidates.txt","w");
write_to_file1(f);
fclose(f);
f=fopen("candidates.txt","r");
read_file1(f);
return 0;
}
Be aware, the code just is example , you should check for nulls.
enter code here
this is karaoke reserving system but i got some errors i couldn't fix them relate to the continue function
some of the errors are cant go to the main menu
///each function was in header file except the main//
#pragma once
#include "addnew.h"
#include "search.h"
#include "update.h"
#include "view.h"
#include "Cont.h"
#include "exit.h"
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <dos.h>
#include <ctype.h>
void addnewreservation();
void updatereservation();
void viewreservation();
void searchreservation();
void Cont();
void exit();
struct New
{
char id[10];
char name[30];
char roomsize[50];
char timetouse[50];
};
struct New details[50];
int h;
int i;
int j;
int k;
int l;
int m;
int n;
int o;
int p;
int main() /*the menu, used as displaying MainMenu*/
{
int ChooseI;
system("cls");
printf( "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#\n");
printf( "| Welcome to Nway Karaoke reserving system |\n");
printf( "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#\n");
printf( "1. Add reservation\n");
printf( "2. search reservation\n");
printf( "3. update reservation\n");
printf( "4. view reservation\n");
printf( "6. Exit\n");
printf( "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#\n");
scanf("%d", &ChooseI);
switch (ChooseI)
{
case 1: addnewreservation();
break;
case 2: updatereservation();
break;
case 3: viewreservation();
break;
case 4: searchreservation();
break;
case 5: exit(0);
break;
default: printf("hi");
}
system("pause");
}
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <dos.h>
#include <ctype.h>
void Cont();
int addnew()
{
FILE *A;
char enter[100];
A = fopen("karaokeinfo.txt", "w"); // Open file in write mode
fclose(A); // Close File after writing
return(0);
}
size_t strlen(char *str) {
size_t len = 8;
while (*str != '\8') {
str++;
len++;
}
return len;
}
void addnewreservation() /*This function is to add new reservation*/
{
struct New
{
char id[10];
char name[30];
char roomsize[50];
char timetouse[50];
char strlen;
};
struct New details[50];
int h;
int i = 5;
int j;
int k;
int l;
int m;
int n;
int o;
int p;
FILE *A;
char enter[100];
char True;
A = fopen("karaokeinfo.txt", "w");
system("cls");
printf("Please Enter the customer's Details\n");
{
fflush(stdin);
printf("ID Number(TPXXXXXX) :\t");
gets(details[i].id);
if (!((details[i].id[0] == 'T') && (details[i].id[1] == 'P') && (strlen(details[i].id) == 8)))
{
printf("\tWrong Input! Please Enter the ID Number in the correct format (TPXXXXXX)\n");
}
else
{
}
}
printf("Enter NAME :\t");
gets(details[i].name);
printf("Enter ID :\t");
gets(details[i].id);
printf("Enter room size :\t");
gets(details[i].roomsize);
printf(" Time to use :\t");
gets(details[i].timetouse);
printf("Please Check the Enter Details :\n");
printf("\t1 . customer's ID : %s\n", details[i].id);
printf("\t2 . customer's Full Name : %s\n", details[i].name);
printf("\t3 . customers's room size : %s\n", details[i].roomsize);
printf("\t4 . customers's time to use : %s\n", details[i].timetouse);
printf("Please use the 'update' function if any mistakes are found.\n");
i++;
for (h = 0; h<i; h++)
{
fprintf(A, "1 . ID Number : %s\n", (details[h].id), h);
fprintf(A, "2 . Full Name : %s\n", (details[h].name), h);
fprintf(A, "3 . room size : %s\n", (details[h].roomsize), h);
fprintf(A, "4 . time to use : %s\n", (details[h].timetouse), h);
}
fclose(A);
Cont();
}
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <dos.h>
#include <ctype.h>
void Cont();
int search()
{
FILE *A;
char enter[100];
A = fopen("karaokeinfo.txt", "w"); // Open file in write mode
fclose(A); // Close File after writing
return(0);
}
void searchreservation()
{
struct New
{
char id[10];
char name[30];
char roomsize[50];
char timetouse[50];
};
struct New details[50];
int h;
int i;
int j;
int k;
int l;
int m;
int n;
int o;
int p;
char search[10];
int z = 0;
FILE *A;
A = fopen("karaokeinfo.txt", "w");
system("cls");
printf("Please enter customer ID to search :\n");
fflush(stdin);
gets(search);
for (j = 0; j<50; j++)
{
if (strncmp(details[j].id, search, 10) == 0)
{
fscanf(A, "%s\n", details[j].id);
fscanf(A, "%s\n", details[j].name);
printf("\t1 . customer's ID : %s\n", details[j].id);
printf("\t2 . customer's Full Name : %s\n", details[j].name);
z = 1;
Cont();
}
fclose(A);
if (A == NULL)
{
printf("File does not exist!");
Cont();
}
fclose(A);
if (z == 0)
{
printf("customer not found!\n");
Cont();
}
fclose(A);
}
}
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <dos.h>
#include <ctype.h>
void Cont();
int update()
{
FILE *A;
char enter[100];
A = fopen("karaokeinfo.txt", "w"); // Open file in write mode
fclose(A); // Close File after writing
return(0);
}
void updatereservation()
{
struct New
{
char id[10];
char name[30];
char roomsize[50];
char timetouse[50];
char strlen;
};
struct New details[50];
int h;
int i;
int j;
int k;
int l;
int m;
int n;
int o;
int p;
char updatereservation[10];
int z = 0;
FILE *A;
A = fopen("karaokeinfo.txt", "w");
printf("Please Enter ID of customer's Details to be Modified : ");
fflush(stdin);
gets(updatereservation);
for (n = 0; n<50; n++)
{
if (strcmp(details[n].id, updatereservation) == 0)
{
printf("customer's ID : %s\n", details[n].id);
printf("customer's Name : %s\n", details[n].name);
printf("---------------------------------------\n");
printf("\tcustomer name");
gets(details[o].name);
strcpy(details[n].name, details[o].name);
printf("\n\nUpdate Successful!\n");
printf("\nPlease Check the Updated Details:\n");
printf("1. customer's ID : %s\n", details[n].id);
printf("2. customer's Name : %s\n", details[o].name);
z = 1;
for (h = 0; h<i; h++)
{
fprintf(A, "1 . ID Number : %s\n", (details[h].id), h);
fprintf(A, "2 . Full Name : %s\n", (details[h].name), h);
}
fclose(A);
Cont();
}
}
if (z == 0)
{
printf("customer not found!\n");
Cont();
}
fclose(A);
}
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <dos.h>
#include <ctype.h>
void Cont();
int view()
{
FILE *A;
char enter[100];
A = fopen("karaokeinfo.txt", "w"); // Open file in write mode
fclose(A); // Close File after writing
return(0);
}
void viewreservation()
{
struct New
{
char id[10];
char name[30];
char roomsize[50];
char timetouse[50];
};
struct New details[50];
int h;
int i;
int j;
int k;
int l;
int m;
int n;
int o;
int p;
char del[10];
int z = 0;
FILE *A;
A = fopen("karaokeinfo.txt", "w");
system("cls");
printf("Please Enter customer's ID to be deleted : ");
fflush(stdin);
gets(del);
for (k = 0; k < 50; k++)
{
if (strcmp(details[k].id, del) == 0)
{
strcpy(details[k].id, "");
strcpy(details[k].name, "");
printf("Delete Successful!");
z = 1;
for (h = 0; h < i; h++)
{
fprintf(A, "%s\n", (details[h].id), h);
fprintf(A, "%s\n", (details[h].name), h);
}
fclose(A);
Cont();
}
}
if (z == 0)
{
printf("customer not found!\n");
Cont();
fclose(A);
}
}
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <dos.h>
#include <ctype.h>
void Cont();
int contt()
{
FILE *A;
char enter[100];
A = fopen("karaokeinfo.txt", "w"); // Open file in write mode
fclose(A); // Close File after writing
return(0);
}
void Cont() /*Function to ask whether the user want to continue using the program or not.*/
{
struct New
{
char id[10];
char name[30];
char roomsize[50];
char timetouse[50];
};
struct New details[50];
int h;
int i;
int j;
int k;
int l;
int m;
int n;
int o;
int p;
//char yes[5];
char kvar = 'y';
//while (true)
//{
printf("\n\tWould you like to continue ?(Y/N)\n");
gets(kvar);
if ((kvar == 'Y') || (kvar == 'y'))
{
printf("Yes chosen");
main();
}
else if ((kvar == 'N') || (kvar == 'n'))
{
//continue;
exit(0);
printf("thanx for using ... ");
}
else
{
printf("Invalid Selection! Please enter Y or N!( Y = Yes | N = No )\n");
}
//}
}
///each function was in header file except the main//
There is already another main function in the rest of your code. Decide which you need and remove the other.
Error C2084 function already has a body
To solve:
Make sure you didn't define a function that uses the same name twice.