it is the all day that I edit my code, but can't find the reason why the last position of my array get overwritten!!
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
typedef struct Person{
char *name;
char *surname;
char *address;
char *number;
} Person;
char * getString(void);
int main(void) {
struct Person *rub = NULL, *ttmp = NULL;
int idx=0,i,j,k;
char c;
char *tmp = NULL;
do{
printf("*******************************\ni - Insert new\n");
printf("n - Find by name\n");
printf("c - Find by surname\n");
printf("e - Delete\n");
printf("p - Print list\n");
printf("0 - Exit\n*******************************\n");
c = getchar();
printf("%d\n",c);
getchar();
switch(c){
case 'i':
ttmp = (struct Person *) realloc(rub, (idx+1)*sizeof(Person));
if(ttmp == NULL){
printf("Cannot allocate more memory.\n");
exit(1);
}
else
rub = ttmp;
printf("Nome: ");
tmp = getString();
rub[idx].name = (char *) malloc(strlen(tmp));
rub[idx].name = tmp;
//printf("Surname: "); //commented in order to test faster
//tmp = getString();
rub[idx].surname = (char *) malloc(strlen(tmp));
rub[idx].surname = tmp;
//printf("Address: ");
//tmp = getString();
rub[idx].address = (char *) malloc(strlen(tmp));
rub[idx].address = tmp;
//printf("Number: ");
//tmp = getString();
rub[idx].number = (char *) malloc(strlen(tmp));
rub[idx].number = tmp;
idx++;
for(k=0;k<idx;k++){
printf("%d) %s %s\n%s\n%s\n-------------------\n", k+1, rub[k].name,rub[k].surname,rub[k].address,rub[k].number);
}
break;
case 'n':
printf("What name are you looking for? ");
scanf("%s",tmp);
for(k=0;k<idx;k++){
if(strcmp(rub[k].name,tmp) == 0){
printf("%s\n%s\n%s\n%s\n", rub[k].name,rub[k].surname,rub[k].address,rub[k].number);
}
}
break;
case 'c':
printf("What surname are you looking for? ");
scanf("%s",tmp);
for(k=0;k<idx;k++){
if(strcmp(rub[k].surname,tmp) == 0){
printf("%s\n%s\n%s\n%s\n", rub[k].name,rub[k].surname,rub[k].address,rub[k].number);
}
}
break;
case 'e':
printf("Select number to delete record:\n ");
for(k=0;k<idx;k++){
printf("%d) %s %s\n", k+1, rub[k].name,rub[k].surname);
}
scanf("%d",&j);
ttmp = NULL;
for(k=0,i=0;k<idx;k++){
if(k+1 != j){
ttmp = (struct Person *) realloc(ttmp, (i+1)*sizeof(Person));
ttmp[i].name = (char *) malloc(strlen(rub[k].name));
ttmp[i].surname = (char *) malloc(strlen(rub[k].surname));
ttmp[i].address = (char *) malloc(strlen(rub[k].address));
ttmp[i].number = (char *) malloc(strlen(rub[k].number));
ttmp[i].name = rub[k].name;
ttmp[i].surname = rub[k].surname;
ttmp[i].address = rub[k].address;
ttmp[i].number = rub[k].number;
i++;
}
}
--idx;
rub = (struct Person *) realloc(ttmp, (idx)*sizeof(Person));
for(k=0;k<idx;k++){
printf("%d/%d) %s %s\n%s\n%s\n-------------------\n", k,idx, rub[k].name,rub[k].surname,rub[k].address,rub[k].number);
}
break;
case 'p':
for(k=0;k<idx;k++){
printf("%d) %s %s\n%s\n%s\n-------------------\n", k+1, rub[k].name,rub[k].surname,rub[k].address,rub[k].number);
}
break;
case '0':
return 0;
break;
}
fseek(stdin,0,SEEK_END);
}
while(c != 0);
return 0;
}
char * getString(void){
char *stringa = NULL, c;
int i=0;
stringa = malloc(sizeof(char));
while((c=getchar()) != '\n'){
stringa = (char *) realloc(stringa, (i+1)*sizeof(char));
stringa[i++] = c;
}
stringa[i] = '\0';
return stringa;
}
Here is my input (please enter the same input, and tell me if you get the same error). I will use some stupid and random words for this example:
i
asd
i
qwe
i
zxc
p
n
asd
p
Here is the output of my last 'p' command:
1) asd asd
asd
asd
-------------------
2) qwe qwe
qwe
qwe
-------------------
3) asd asd
asd
asd
-------------------
Why the last position of the array became the same as the first??
Any other advice or suggestion for the code will be welcome!!
thanks
Why the last position of the array became the same as the first?
case 'n':
printf("What name are you looking for? ");
scanf("%s",tmp);
tmp are reused. (case 'c': too)
change to
E.g
case 'n':
printf("What name are you looking for? ");
tmp=getString();
for(k=0;k<idx;k++){
if(strcmp(rub[k].name,tmp) == 0){
printf("%s\n%s\n%s\n%s\n", rub[k].name,rub[k].surname,rub[k].address,rub[k].number);
}
}
free(tmp);
break;
and
rub[idx].name = (char *) malloc(strlen(tmp));
rub[idx].name = tmp;
//As well as in other like
memory leak.
only
rub[idx].name = tmp;//by getString()
I din't read the whole code but found this code that seems incorrect:
stringa = malloc(sizeof(char));
while((c=getchar()) != '\n'){
stringa = (char *) realloc(stringa, sizeof(char));
You are realloc-ing always one byte, the string nevers grows up!
You should realloc depending on the variable i
Related
Hey I have a problem with my code project where I try to create a project that keeps up wit Olympic medals. I have a problem of creating a text file that contains the structure and is named by user. I also have a problem to download the structure.
PROBLEM: I have a problem to make a text file named by user that contains the structure and then download it back to stdout. I don't know how to fix my functions to do this correctly. Now my function save_file can't even produce the file.
Example if the input:
A Canada
A USA
M USA 2 1 1
M Canada 0 0 1
M USA 1 3 1
M USA -1 0 0
L
W medals
Q
I have defined my structure this way:
typedef struct Olympia
{
char* country;
int gold;
int silver;
int bronze;
}Olympia;
Then I have a function that adds country
int add_country(struct Olympia* data, char* str, int i)
{
if (str[0] == '\0') //checking that input is correct
{
printf("Error! Try again!\n");
}
else
{
data[i].country = malloc(strlen(str) + 2); //allocating memory for country name
strcpy(data[i].country, str); //adding country to database
data[i].gold = 0; //setting medals to zero
data[i].silver = 0;
data[i].bronze = 0;
i++;
}
return i;
}
Next I add medals to the each country
int update_medals(struct Olympia* data, char* str, int add_gold, int add_silver, int add_bronze, int i)
{
int a = 0;
int b = 0;
if (str[0] == '\0') //checking that input is correct
{
printf("Error! Try again!");
}
else
{
while (a < i)
{
if (strcmp(data[a].country, str) == 0) //adding medals to right country
{
data[a].gold = data[a].gold + add_gold;
data[a].silver = data[a].silver + add_silver;
data[a].bronze = data[a].bronze + add_bronze;
b++;
}
a++;
}
if (b == 0) //and if the country didn't participate to the olympics
{
printf("This country isn't in the Olympics! Try Again!\n");
}
}
}
Next there is print function
int print_data(struct Olympia* data, int i)
{
for (int a = 0; a < i; a++)
{
printf("%s %d %d %d\n", data[a].country, data[a].gold, data[a].silver, data[a].bronze);
}
}
And then there are the two function that doesn't work. What should I do?
Olympia *save_file(Olympia* data, const char* filename, int i)
{
if (strlen(filename) > 100)
{
printf("Filename is too long: Maxium lenght for filename is 100 characters");
return data;
}
char name[100];
int ret = sscanf(filename, "W %s", name);
if (ret != 1)
{
printf("Error! Try again!");
return data;
}
FILE* file = fopen(name, "w");
if (!file)
{
printf("Error saving file! Try again");
return data;
}
int a = 0;
while (data[a].country[0] != 0)
{
fprintf(file, "%s %d %d %d\n", data[a].country, data[a].gold, data[a].silver, data[a].bronze);
a++;
}
fclose(file);
return 0;
}
int load_file(struct Olympia* data, char* filename, int i)
{
int a = 0;
FILE* file = fopen(filename, "r");
if (!file)
{
printf("Error opening file! Try again");
}
struct Olympia* arr = malloc(sizeof(Olympia));
while (fscanf(file, "%s %d %d %d", data[a].country, data[a].gold, data[a].silver, data[a].bronze))
{
i++;
a++;
arr = realloc(arr, sizeof(Olympia) * (i + 2));
}
arr[a].country[0] = 0;
fclose(file);
return arr;
}
And the main function
int main(void)
{
char command;
int gold = 0;
int silver = 0;
int bronze = 0;
int i = 0;
char* line = (char*)malloc((100) * sizeof(char)); //allocating memory for one stdin line
char* countryname = (char*)malloc(20 * sizeof(char)); // allocating memory for country name
char* filename = (char*)malloc(100 * sizeof(char));
struct Olympia* countrydata = malloc(sizeof(struct Olympia) * 1); //allocating memory for structure
line = fgets(line, 100, stdin);
while(1)
{
sscanf(line, "%c %s %d %d %d", &command, countryname, &gold, &silver, &bronze);
switch (command)
{
case 'A':
i = add_country(countrydata, countryname, i);
countrydata = realloc(countrydata, sizeof(struct Olympia) * (i + 1));
break;
case 'M':
update_medals(countrydata, countryname, gold, silver, bronze, i);
break;
case 'L':
print_data(countrydata, i);
break;
case 'W':
save_file(countrydata, filename, i);
break;
case 'O':
i = load_file(countrydata,filename, i);
break;
case 'Q':
free(line);
free(countryname);
free(countrydata);
return(EXIT_SUCCESS);
}
line = fgets(line, 100, stdin);
if (line == NULL)
{
free(line);
free(countryname);
free(countrydata);
return(EXIT_SUCCESS);
}
}
}
You call save_file(countrydata, filename, i); without having set filename. Change to save_file(countrydata, line, i); since for whatever reason you expect the command character W to precede the name.
Then in save_file() the condition in while (data[a].country[0] != 0) is unusable, since the data element after the last one is not initialized. Use while (a < i) instead.
Welcome, I have problem with my while-loop. It only reads first line from file.txt. Before adding funcition to split my line by strtok_r ,my apps doesnt work good.The purpose of this while-loop is reads all line from file and split everysingle line by "|" and these parts add to appropriate tabs.
What should i do?
Its my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mem.h>
#include <assert.h>
struct movie {
char title[100];
int year;
char genre[100];
};
int i = 0;
struct movie *movies[50];
int porownaj(struct movie* first, struct movie* second)
{
if(first->year==second->year){
if(first->title>second->title){
return 1;
}
if(first->title<second->title){
return -1;
}
if(first->title==second->title){
if(first->genre>second->genre){
return 1;
}
if(first->genre<second->genre){
return -1;
}
if(first->genre==second->genre){
return 0;
}
}
}
if(first->year>second->year){
return 1;
}
if(first->year<second->year){
return -1;
}
if(first->title==second->title){
if(first->year>second->year){
return 1;
}
if(first->year<second->year){
return -1;
}
if(first->year==second->year){
if(first->genre>second->genre){
return 1;
}
if(first->genre<second->genre){
return -1;
}
if(first->genre==second->genre){
return 0;
}
}
}
if(first->title>second->title){
return 1;
}
if(first->title<second->title){
return -1;
}
if(first->genre==second->genre){
if(first->year>second->year){
return 1;
}
if(first->year<second->year){
return -1;
}
if(first->year==second->year){
if(first->title>second->title){
return 1;
}
if(first->title<second->title){
return -1;
}
if(first->title==second->title){
return 0;
}
}
}
if(first->genre>second->genre){
return 1;
}
if(first->genre<second->genre){
return -1;
}
}
void zamien(struct movie* movies[50], int x, int y)
{
struct movie* tmpMovie = movies[x];
movies[x] = movies[y];
movies[y] = tmpMovie;
}
int main()
{
struct movie *movies[50];
do
{
printf("\n");
printf("Projekt nr 6 \n");
printf("\n");
printf("\b MENU GLOWNE \n");
printf("|-----------------| \n");
printf("[1]Baza filmow \n");
printf("[2]Dodaj film \n");
printf("[3]Zakoncz Program \n");
printf("|-----------------| \n");
char znak;
printf("Wybierz opcje z menu: ");
scanf("%s", &znak);
system("cls");
switch (znak)
{
case '1':
system("cls");
printf("Wybrales opcje BAZA FILMOW \n");
printf("TYTUL | ROK | GATUNEK\n");
printf("----------------------\n");
FILE *baza;
if ((baza = fopen("movies.txt", "r")) == NULL)
{
system("cls");
printf("Blad otwarcia pliku\n");
exit(1);
}
char tekst[512];
char *title = NULL;
char *year = NULL;
char *genre = NULL;
char *buffer = NULL;
int x = 0;
while (fgets(tekst, 512, baza) != NULL)
{
printf("%s", tekst);
title = strtok_r(tekst, "|", &buffer);
assert(title != NULL);
strcpy(movies[x]->title, title);
year = strtok_r(NULL, "|", &buffer);
assert(year != NULL);
movies[x]->year = atoi(year);
genre = strtok_r(NULL, "|", &buffer);
assert(genre != NULL);
strcpy(movies[x]->genre, genre);
x++;
}fclose(baza);
do
{
printf("\n[1]Sortuj wg. tytulu");
printf("\n[2]Sortuj wg. roku produkcji");
printf("\n[3]Sortuj wg. gatunku filmu");
printf("\n[4]Wroc do glownego menu\n");
printf("|-----------------|\n");
char click;
printf("\nWybierz opcje: ");
scanf("%s", &click);
switch (click)
{
case '1':
system("cls");
printf("Sorotowanie wg. tytulu");
/*
for(int i=0;i<10-1;i++){
for(int j=0;j<10-i-1;j++){
if(porownaj(&title[j],&title[j+1])>0){
zamien(&title[j],&title[j+1]);
}
}
}
for(int i=0;i<10;i++){
printf("(%d, %d)", title[i].x, );
}
*/
break;
case '2':
system("cls");
printf("Sorotowanie wg. roku produkcji");
/*
for(int i=0;i<10-1;i++){
for(int j=0;j<10-i-1;j++){
if(porownaj(&year[j],&year[j+1])>0){
zamien(&year[j],&year[j+1]);
}
}
}
for(int i=0;i<10;i++){
printf("(%d, %d)", year[i].x, );
}
*/
break;
case '3':
system("cls");
printf("Sorotowanie wg. gatunku filmu");
break;
case '4':
system("cls");
return main();
default:
system("cls");
printf("Nie ma takiej opcji. Program sie wylaczy!");
return 0;
}
} while (1);
case '2':
system("cls");
printf("Wybrales opcje DODAJ FILM \n");
movies[i] = malloc(sizeof(movie));
printf("Podaj nazwe filmu: ");
while ((getchar()) != '\n');
gets(movies[i]->title);
printf("Podaj rok produkcji: ");
scanf("%d", &(movies[i]->year));
printf("Podaj gatunek filmu: ");
while ((getchar()) != '\n');
gets(movies[i]->genre);
fflush(stdin);
FILE *plik = fopen("movies.txt", "a+");
fprintf(plik, "%s | %d | %s \n", movies[i]->title, movies[i]->year, movies[i]->genre);
fclose(plik);
i++;
printf("Zapisane..");
continue;
case '3':
system("cls");
printf("ZAKONCZ PROGRAM \n");
return 0;
default:
printf("Nie ma takiej opcji! \n");
return main();
}
}while (1);
return (0);
}
movies.txt:
Title | 1234 | Scifi
Title_2 | 4321 | SciFI
I bet that every line is printed with printf("%s", tekst);.
But you forget that strtok does not make a copy of the string. Thus (if you have your assignments fixed the other way around), you still assign poinetrs into the same tekst variable to all your movies. Instead allocate memory and copy the strings:
title = strtok_r(tekst, "|", &buffer);
movies[x]->title= malloc(strlen(title)+1);
strcpy(movies[x]->title, title);
and the same for the other string variables.
There are a number of bugs in your program.
To start with this:
char title = NULL;
char year = NULL;
char genre = NULL;
together with this:
title = strtok_r(tekst, "|", &buffer);
year = strtok_r(NULL, "|", &buffer);
genre = strtok_r(NULL, "|", &buffer);
int year = atoi(year);
You define title as a char but strtok_r returns a char*. Consequently you overwrite memory by storing a "char pointer" in a "char" variable.
This could destroy the file-pointer. The error 0xC0000005 you get (as you mention in a comment) could indicate that:
Application Error 0xc0000005 (Access Violation) error is usually caused by your computer not being able to correctly process the files...
Change the definition to:
char *title = NULL;
char *year = NULL;
char *genre = NULL;
Further:
year = strtok_r(NULL, "|", &buffer); // A variable named year
genre = strtok_r(NULL, "|", &buffer);
int year = atoi(year); // A NEW variable with same name!!
Change it to:
year = strtok_r(NULL, "|", &buffer);
genre = strtok_r(NULL, "|", &buffer);
int yearN = atoi(year); // Use another name
Further you should always check that year is not NULL before calling atoi
So my suggestion is to simplify your code for debugging. Try:
FILE *baza;
if ((baza = fopen("movies.txt", "r")) == NULL)
{
system("cls");
printf("Blad otwarcia pliku\n");
exit(1);
}
char tekst[512];
char* title = NULL;
char* year = NULL;
char* genre = NULL;
char* buffer = NULL;
int x = 0;
printf("while-loop start\n");
while (fgets(tekst, 512, baza) != NULL)
{
printf("%s", tekst);
//x++;
//title = strtok_r(tekst, "|", &buffer);
//year = strtok_r(NULL, "|", &buffer);
//genre = strtok_r(NULL, "|", &buffer);
//int year = atoi(year);
//title=movies[x]->title;
//year=movies[x]->year;
//genre=movies[x]->genre;
}
printf("while-loop complete\n");
Now check the output you get from this code. If that works as expected, you can start to un-comment lines one-by-one.
In the end I guess your function should look like:
FILE *baza;
if ((baza = fopen("movies.txt", "r")) == NULL)
{
system("cls");
printf("Blad otwarcia pliku\n");
exit(1);
}
char tekst[512];
char* title = NULL;
char* year = NULL;
char* genre = NULL;
char* buffer = NULL;
int x = 0;
while (fgets(tekst, 512, baza) != NULL)
{
printf("%s", tekst);
title = strtok_r(tekst, "|", &buffer);
assert(title != NULL);
strcpy(movies[x]->title, title);
year = strtok_r(NULL, "|", &buffer);
assert(year != NULL);
movies[x]->year = atoi(year);
genre = strtok_r(NULL, "|", &buffer);
assert(genre != NULL);
strcpy(movies[x]->genre, genre);
x++;
}
The above assumes that you have already allocated memory for all pointers in movies. Like:
struct movie { char title[100]; int year; char genre[100]; };
int main() {
int i = 0;
struct movie *movies[50];
for(i=0; i<50; ++i) movies[i] = malloc(sizeof *movies);
. . .
out of any other possible problems these lines are problematic :
title=movies[x]->title;
year=movies[x]->year;
genre=movies[x]->genre;
as it is said in remark the assignments must be reversed ( movies[x]->foo = foo;)
you need to duplicate the strings else you save a sub addresses of tekst which is modified each loop and may be disappeared when you will use them
so
movies[x]->title = strdup(title);
movies[x]->year = year;
movies[x]->genre = strdup(genre);
Or course if movies[x]->title and movies[x]->genre are char[] rather than char* the code have to copy rather than duplicate :
strcpy(movies[x]->title, title);
movies[x]->year = year;
strcpy(movies[x]->genre, genre);
and if needed replace strcpy by strncpy to not overflow
So i need to write and read to and from a binary file, but cant seem to do it, and when using readFromFile, the program crashes. I need help write to binary file, and then reading from it and resuming my work later after turning off the program.I have no idea what i am doing wrong, and i have googled for a long time now, but with no results. Here is the code of my program:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char *subjName;
char *lectName;
char *lectSurname;
int credits;
int num_students;
}Subject;
typedef struct{
Subject **subjs;
int num_subjs;
}Subjects;
int numOfSubjs=0;
void listInput();
void listEdit();
void listDelete();
void listPrint();
int userChoice(int select);
int enterNumber(char *name);
void saveToFile(Subjects *subjects);
void readFromFile(Subjects *subjects);
int main() {
Subjects *subjects = malloc(sizeof(Subjects));
subjects->num_subjs = 0;
subjects->subjs = NULL;
readFromFile(subjects);
int r=1;
while(r!=0){
int select=userChoice(select);
switch(select){
case 1:
listPrint(subjects);
break;
case 2:
listInput(&subjects);
break;
case 3:
listDelete(subjects);
break;
case 4:
listEdit(subjects);
break;
case 0:
r=0;
break;
}
}
saveToFile(subjects);
return 0;
}
int userChoice(int select){ // menu options
int choice,input=0;
printf("(1). View all the data\n");
printf("(2). Enter new data\n");
printf("(3). Delete data\n");
printf("(4). Edit data\n");
printf("(0). Exit\n");
printf("-----------------------------\n");
while(input!=1){
choice = enterNumber("menu");
if(choice>4 || choice<0){
printf("Invalid input \n");
}
else
input = 1;
}
return choice;
}
void listPrint(Subjects *subjects){ // print data
int i;
for(i=0; i< numOfSubjs; i++){
printf("%d, %s, %s, %s, %d, %d\n",i+1, subjects->subjs[i]->subjName, subjects->subjs[i]->lectName, subjects->subjs[i]->lectSurname, subjects->subjs[i]->credits, subjects->subjs[i]->num_students);
}
printf("Number of entries: %d \n", numOfSubjs);
}
char *getln() //dynamically allocate input string
{
char *line = NULL, *tmp = NULL;
size_t size = 0, index = 0;
int ch = 1;
while (ch) {
ch = getc(stdin);
if (ch == '\n')
ch = 0;
if (size <= index) {
size += 1;
tmp = realloc(line, size);
if (!tmp) {
free(line);
line = NULL;
break;
}
line = tmp;
}
line[index++] = ch;
}
return line;
}
void saveToFile(Subjects *subjects){
FILE *data;
data = fopen("data.bin","wb");
printf("%s", subjects->subjs[0]);
for(int i=0; i<numOfSubjs; i++){
fwrite(&subjects->subjs[i],sizeof(Subject*),1,data);
}
fclose(data);
}
void readFromFile(Subjects *subjects){
FILE *data;
int i=0;
data = fopen("data.bin","rb");
while(!feof(data))
{
fread(&subjects->subjs[i],sizeof(Subject*),1,data);
i++;
}
fclose(data);
}
int isText(char *str,char *name){ // check if is text
for(int i = 0; i < strlen(str);i++){
if((str[i]<'A' || str[i]>'z') && str[i]!=' '){
printf("Error, %s must be a text \n",name);
return 0;
}
}
return 1;
}
int enterNumber(char *name){ // enter number and check if is number
int input=0, crash=0, num=0;
while(input!=1)
{
crash=0;
printf("Enter the number of %s\n", name);
scanf("%d", &num);
while(getchar()!='\n')
{
crash++;
}
if(crash>0 || num<0)
printf("Error, enter a not negative number of %s\n", name);
else if(crash==0)
input=1;
}
return num;
}
void listInput(Subjects **p_subjects){ // input new data
Subject *new_subj = malloc(sizeof(Subject));
new_subj->subjName = NULL;
new_subj->lectName = NULL;
new_subj->lectSurname = NULL;
new_subj->credits = 0;
new_subj->num_students = 0;
do{
printf("Enter the name of the subject \n");
new_subj->subjName = getln();
}while(!isText(new_subj->subjName,"Subject name"));
do{
printf("Enter the name of the lecturer \n");
new_subj->lectName = getln();
new_subj->lectName[0] &= '_';
}while(!isText(new_subj->lectName,"Lecturer's name"));
do{
printf("Enter the surname of the lecturer\n");
new_subj->lectSurname = getln();
new_subj->lectSurname[0] &= '_'; //Convert to uppercase if lowercase
}while(!isText(new_subj->lectSurname,"Lecturer's name"));
new_subj->credits = enterNumber("credits");
new_subj->num_students = enterNumber("students");
(*p_subjects)->subjs = realloc((*p_subjects)->subjs,sizeof(Subject*)*(++(*p_subjects)->num_subjs));
(*p_subjects)->subjs[(*p_subjects)->num_subjs-1] = new_subj;
numOfSubjs++;
printf("Added a new entry.\n\n");
}
void listDelete(Subjects *subjects){ // delete entries
int del;
if(numOfSubjs==0)
printf("Number of entries is 0, can't delete anything\n");
else{
printf("Enter 0 to exit. Number of subjects : %d \n", numOfSubjs);
while(1){
del = enterNumber("entry which you would like to delete");
if(del<=numOfSubjs && del>0){
for(int i = del-1; i<numOfSubjs-1; i++){
subjects->subjs[i]=subjects->subjs[i+1];
subjects->subjs = realloc(subjects->subjs,sizeof(Subject*)*(--subjects->num_subjs));
}
numOfSubjs--;
break;
}
if(del>numOfSubjs)
printf("Error, input a number between 1 and %d (or enter 0 to exit)\n", numOfSubjs);
else
break;
}
}
}
void listEdit(Subjects *subjects){ // edit entries
int choice=0, editEntry=0, editSubj=0;
if(numOfSubjs == 0)
printf("Number of entries is 0, can't edit anthing\n");
else{
while(1){
printf("Number of entry must be between 1 and %d \n", numOfSubjs);
choice = enterNumber("entry you would like to edit.");
if(choice>0 && choice<=numOfSubjs){
while(1){
editEntry = enterNumber("what would you like to edit\n 1 - Subject name\n 2 - Lecturer's name\n 3 - Lecturer's surname\n 4 - Number of credits\n 5 - Number of students");
if(editEntry>0 && editEntry <=5){
switch(editEntry){
case 1:
do{
printf("Enter the name of the subject \n");
subjects->subjs[choice-1]->subjName = getln();
}while(!isText(subjects->subjs[choice-1]->subjName,"Subject name"));
break;
case 2:
do{
printf("Enter Lecturer's name \n");
subjects->subjs[choice-1]->lectName = getln();
}while(!isText(subjects->subjs[choice-1]->lectName,"Lecturer's name"));
break;
case 3:
do{
printf("Enter Lecturer's surname \n");
subjects->subjs[choice-1]->lectSurname = getln();
}while(!isText(subjects->subjs[choice-1]->lectSurname,"Lecturer's surname"));
break;
case 4:
subjects->subjs[choice-1]->credits = enterNumber("credits");
break;
case 5:
subjects->subjs[choice-1]->num_students = enterNumber("students");
break;
}
}
break;
}
}
break;
}
}
}
&subjects->subjs[i], This is Undefined behavior. Accessing garbage value. You need to properly initialize it proper memory address. You ddin't do it anywhere.
(*subjects).subjs or subjects->subjs -> This is not pointing anywhere. It is NULL.
Also here you don't need the double pointer. A single pointer would do the thing you want to do.
typedef struct{
Subject *subjs;
int num_subjs;
}Subjects;
For single pointer this would be like
Subjects *subjects = malloc(sizeof(Subjects));
subjects->num_subjs = 10;
subjects->subjs = malloc(subjects->num_subjs * sizeof Subject);
subjects->subjs[0].subjName = malloc(40);
Each of the malloc should be checked with it's return Value. If it's NULL then it would be error to proceed further or access it.
And free() it accordingly when you are done working with it.
Some basic things:-
typedef struct{
Subject *subjs;
int num_subjs;
}Subjects;
Now let's look a bit in the code.
Op asked why OP should initialize and isn;t subjects->num_subjs = 0;
subjects->subjs = NULL; not enough?
A pointer is a variable that is supposed to hold address. here Subject* will hold the address of the variables of type Subject.
Now here initially you initialized it.
You have allocated a memory and assigned it's address to the Subject* variable subjects.
Now let's see what else you do.
subjects->num_subjs = 0;
subjects->subjs = NULL;
You initialized it. And then you try to access it(subjects->subjs[i]). Can you tell me where it points to? (subject->subjs)?
Answer is nope. It is pointing to nowhere. It contains NULL value now. Don't you think you should tell it how many subject you want to hold and allocate accordingly? Yes you should and that's what I did precisely in the example shown.
Whenever you have a pointer variable ask yourself what it contains - and if the value is something you know about, not some random garbage value.
I've been writing a HTML generator in C. After some research I found some code that allows me to take input in a array that allocates more memory to it if you need it. The problem is that after a couple of times, the code stops working.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *inputString(FILE *fp, size_t size);
int main(){
char *nameOfFile;
char *multiF;
int choice;
FILE *f;
printf("Name of file: ");
nameOfFile=inputString(stdin, 10);
f = fopen(strcat(nameOfFile,".html"),"w+");
fprintf(f, "<head>\n");
printf("Title: ");
multiF=inputString(stdin, 10);
fprintf(f, "\t<title>%s</title>\n</head>\n<body>\n", multiF);
while(choice != 99){ //Code stops working in while loop
printf("What do you want to do?\n");
printf("[1]Headings\n");
printf("[2]Paragraphs\n");
printf("[99]Exit\n");
scanf("%d",&choice);
switch(choice){
case 1:
printf("What size[1-6]? ");
scanf("%d",&choice);
if(choice < 7 && choice > 0){
printf("Heading[%d]:",choice);
multiF=inputString(stdin,10);
fprintf(f,"<h%d>%s</h%d>",choice,multiF,choice);
}else{
printf("Input something useful...\n");
}
break;
case 2:
printf("Paragraph: ");
multiF=inputString(stdin,10);
fprintf(f,"<p>%s</p>",multiF);
break;
case 99:
break;
default:
printf("Input something useful...\n");
}
}
fprintf(f,"</body>");
free(multiF);
free(nameOfFile);
return 0;
}
char *inputString(FILE *fp, size_t size){
char *str;
int ch;
size_t len=0;
str = realloc(NULL, sizeof(char)*size);
if(!str)return str;
while(EOF!=(ch=fgetc(fp)) && ch != '\n'){
str[len++]=ch;
if(len==size){
str = realloc(str, sizeof(char)*(size+=16));
if(!str)return str;
}
}
str[len++]='\0';
return realloc(str, sizeof(char)*len);
}
This should work:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void inputString(char **str, FILE *fp); // No char *str, because we need to change the value of pointer str
int main(){
char *nameOfFile = malloc(0);
char *multiF = malloc(0); // Calling malloc() for later free()
int choice = 0; // Initialization
FILE *f;
printf("Name of file: ");
inputString(&nameOfFile, stdin);
nameOfFile = realloc(nameOfFile, strlen(nameOfFile) + 5); // You need more memory for ".html"
strcat(nameOfFile, ".html");
f = fopen(nameOfFile, "w");
fprintf(f, "<head>\n");
printf("Title: ");
inputString(&multiF, stdin);
fprintf(f, "\t<title>%s</title>\n</head>\n<body>\n", multiF);
while(choice != 99){
printf("What do you want to do?\n");
printf("[1]Headings\n");
printf("[2]Paragraphs\n");
printf("[99]Exit\n");
scanf(" %d%*c", &choice); // " %d" to discard whitespace characters, and "%*c" to consume '\n'
switch(choice){
case 1:
printf("What size[1-6]? ");
scanf(" %d%*c", &choice);
if(choice < 7 && choice > 0){
printf("Heading[%d]:", choice);
inputString(&multiF, stdin);
fprintf(f, "<h%d>%s</h%d>", choice, multiF, choice);
}else{
printf("Input something useful...\n");
}
break;
case 2:
printf("Paragraph: ");
inputString(&multiF, stdin);
fprintf(f, "<p>%s</p>", multiF);
break;
case 99:
break;
default:
printf("Input something useful...\n");
break;
}
}
fprintf(f, "</body>");
free(multiF);
free(nameOfFile);
fclose(f); // Don't forget to close the file
return 0;
}
void inputString(char **str, FILE *fp){
int ch;
size_t size = 1, index = 0;
free(*str);
*str = malloc(size);
if(!*str)
return;
while(EOF != (ch = fgetc(fp)) && ch != '\n'){
(*str)[index++] = ch;
if(index == size){
*str = realloc(*str, size += 16); // sizeof (char) is guaranteed to be 1 by the standard
if(!*str)
return;
}
}
(*str)[index] = '\0'; // No need to increase index
}
See my comments for the details.
There are some issues in your code:
After scanf("%d",&choice);, a '\n' will be left in stdin. When it is met by inputStrng(), the function stops generating string immediately.
To change the value of a pointer variable in a function, you need to pass a pointer to pointer, such as char **str.
You forgot to close the file, which may cause information loss.
I am making a linked list of objects. For some reason when i go to print the value of a value within the object (a char array) i'm getting junk printing before the value. Why is this happening and how do i get rid of it? Here is a part of my code:
int num = 0;
int input = 1;
int retval = 0;
struct PDB2 *llist;
char avalue[100] = "";
llist = (struct PDB2 *)malloc(sizeof(struct PDB2));
llist->data1[100] = NULL;
llist->next = NULL;
while(input != 0) {
printf("\n-- Menu Selection --\n");
printf("0) Quit\n");
printf("1) Insert\n");
printf("2) Delete\n");
printf("3) Search\n");
printf("4) Display\n");
scanf("%d", &input);
switch(input) {
case 0:
default:
printf("Goodbye ...\n");
input = 0;
break;
case 1:
printf("Your choice: `Insertion'\n");
printf("Enter the value which should be inserted: ");
scanf("%s", &avalue);
append_node(llist, avalue);
break;
case 2:
printf("Your choice: `Deletion'\n");
printf("Enter the value which should be deleted: ");
scanf("%s", &avalue);
delete_node(llist, avalue);
break;
case 3:
printf("Your choice: `Search'\n");
printf("Enter the value you want to find: ");
scanf("%s", &avalue);
if((retval = search_value(llist, avalue)) == -1)
printf("Value `%s' not found\n", avalue);
else
printf("Value `%s' located at position `%d'\n", avalue, retval);
break;
case 4:
printf("You choice: `Display'\n");
display_list(llist);
break;
} /* switch */
} /* while */
free(llist);
return(0);
}
void append_node(struct PDB2 *llist, char message[])
{
int x = 0;
while(llist->next != NULL)
{
llist = llist->next;
}
llist->next = (struct PDB2 *)malloc(sizeof(struct PDB2));
for(x = 0; x < 100; x++)
{
llist->next->data1[x] = message[x];
}
llist->next->next = NULL;
}
void display_list(struct PDB2 *llist)
{
while(llist->data1 == NULL)
{
llist = llist->next;
}
while(llist->next != NULL)
{
printf("%s ", llist->data1);
llist = llist->next;
}
printf("%s", llist->data1);
}
The char array in your initial node is uninitialized. Change
llist->data1[100] = NULL;
to
llist->data1[0] = '\0';
to stop it from being printed.
These two lines, together, are wrong:
char avalue[100] = "";
[...]
scanf("%s", &avalue);
The proper way to scanf a string is:
scanf("%s", avalue); // No Address-Of operator.
The important part of your code (based on your description of the problem) can be boiled down to this:
struct PDB2 {
char data[100];
...other fields...
};
struct PDB2 * llist = (struct PDB2 *)(malloc(sizeof(struct PDB2)));
llist->data[100] = NULL; /* This is wrong */
printf("%s", llist->data); /* This prints rubbish */
The marked line is wrong because:
NULL is meant to be used as a pointer value, not as a character. You probably mean to use '\0'.
The point seems to be to clear the string, so the correct method is
.
llist->data[0] = '\0';
Also, the call to scanf seems that it should be
char avalue[100];
scanf("%s", avalue);
since avalue is already of type char *. With the extra & you may be writing to an invalid location. However,
int input;
scanf("%d", &input);
is correct for integer input.