I am new to c language. I add the structure members(int dd) to file(file.txt) and now am trying to read that file to access structure members(Day.dd). But when I add 5 to a variable through scanf() function and compile that file and run, I did not get (Day = 5). I got Day = 1325715504
My code is:
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
struct DailyCost
{
int dd;
} Day;
FILE *fp;
void add_dailycost();
void display();
void mainmenue();
int main()
{
int j;
fp = fopen("/home/mohit/projects/c/gedit_Dailyost/TextFiles/fileb.txt", "w+");
rewind(fp);
mainmenue();
return 0;
}
void mainmenue()
{
struct DailyCost day1;
int j;
printf("Enter 1 to add dailycost and enter 2 display dailycost.");
scanf("%d", &j);
switch (j)
{
case 1:
add_dailycost(day1);
break;
case 2:
display(day1);
break;
}
}
void add_dailycost(struct DailyCost Day)
{
if (fp != NULL)
{
fseek(fp, 0L, SEEK_END);
}
int j;
printf("Enter Day = ");
scanf("%d", &Day.dd);
fwrite(&Day, sizeof(struct DailyCost), 1, fp);
printf("If menmenue press 1 and exit press 0 = ");
scanf("%d", &j);
if (j == 1)
{
mainmenue();
}
else
{
printf("\nThank you");
fclose(fp);
exit(0);
}
}
void display(struct DailyCost Day)
{
fread(&Day, sizeof(struct DailyCost), 1, fp);
rewind(fp);
printf("Day = %d\n", Day.dd);
fclose(fp);
}
Please suggest what I had done wrong to code a program. And how to fix this problem, by taking my code.
As suggested by Klaus Gütter use call by reference, to make changes to original object.
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
struct DailyCost
{
int dd;
} Day;
FILE *fp;
void add_dailycost();
void display();
void mainmenue();
int main()
{
int j;
fp = fopen("/home/mohit/projects/c/gedit_Dailyost/TextFiles/fileb.txt", "w+");
rewind(fp);
mainmenue();
return 0;
}
void mainmenue()
{
struct DailyCost day1;
int j;
printf("Enter 1 to add dailycost and enter 2 display dailycost.");
scanf("%d", &j);
switch (j)
{
case 1:
add_dailycost(&day1);
break;
case 2:
display(&day1);
break;
}
}
void add_dailycost(struct DailyCost* Day) /*if you pass simply
struct DailyCost , all the contents of original objects will be copied
into a new memory area, to be used in this function and will be
destroyed when this function exits. */
/* if you pass just the address of your object,you are working on
the same memory area as you passed from,in this case from mainmenue. */
{
if (fp != NULL)
{
fseek(fp, 0L, SEEK_END);
}
int j;
printf("Enter Day = ");
scanf("%d", &Day->dd);
fwrite(Day, sizeof(struct DailyCost), 1, fp);
printf("If menmenue press 1 and exit press 0 = ");
scanf("%d", &j);
if (j == 1)
{
mainmenue();
}
else
{
printf("\nThank you");
fclose(fp);
exit(0);
}
}
void display(struct DailyCost* Day) /* passing the object instead
of reference would also work here. But just for the display
purpose, you need not to unnecessarily create a new memory. */
{
fread(Day, sizeof(struct DailyCost), 1, fp);
rewind(fp);
printf("Day = %d\n", Day->dd);
fclose(fp);
}
Read about call by value and call by reference, and passing struct as argument in the internet, if you are not clear yet.
Related
I'm currently working on a program that is suppose to read information from a text-file. The text-file contains a song list and I have created a struct that should be able to hold each song in a song list in my program.
I have divided the program in different files and they look something like this.
main.c
#include "FuncDek.h"
#include <locale.h>
#include <crtdbg.h>
int main()
{
//For swedish and check memory leak
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
setlocale(LC_ALL, "swedish");
//Create with starting size of 5
Song *ptr = (Song *)malloc(sizeof(Song) * 4);
int menuChoice = 0;
int nrOfSongs = 0;
//Read from file
readFromFile(ptr, &nrOfSongs);
system("pause");
do
{
system("cls");
menuChoice = menu();
switch (menuChoice)
{
case 1:
addSong(ptr, &nrOfSongs);
break;
case 2:
showList(ptr, nrOfSongs);
break;
case 0:
break;
default:
printf("\nFelaktigt val, försök igen\n");
system("pause");
break;
}
} while (menuChoice != 0);
free(ptr);
system("pause");
return 0;
}
FuncDek.h
#ifndef FUNCDEK
#define FUNCDEK
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char title[25];
char artist[25];
char year[4];
} Song;
int menu();
void addSong(Song *ptr, int *nrOfSongs);
void showList(Song *ptr, int nrOfSongs);
void readFromFile(Song *ptr, int *nrOfSongs);
#endif
and finally the FuncDek.c
#include "FuncDek.h"
#pragma warning(disable: 4996)
//Print all from list
void showList(Song *ptr, int nrOfSongs)
{
system("cls");
printf("Låtlista\n");
printf("-------------------------------\n");
for (int i = 0; i < nrOfSongs; i++)
{
printf("Title: %s", ptr[i].title);
printf("Artist: %s", ptr[i].artist);
printf("År: %s \n\n", ptr[i].year);
}
system("pause");
}
//Read from file
void readFromFile(Song *ptr, int *nrOfSongs)
{
FILE *fileOpen;
fileOpen = fopen("song.txt", "r+");
if (fileOpen == NULL)
{
printf("Something went wrong. Could't open file\n");
}
char line[100];
int counter = 0;
int nrOfSongsInList = 0;
/*Read all information to line*/
while (fgets(line, sizeof(line), fileOpen) != NULL)
{
if (counter == 0)
{
nrOfSongsInList = line[0] - '0';
counter++;
}
else if (counter % 2 == 1 && counter == 1)
{
strcpy(ptr[*nrOfSongs].title, line);
counter++;
}
else if (counter % 2 == 0)
{
strcpy(ptr[*nrOfSongs].artist, line);
counter++;
}
else if (counter % 3 == 0)
{
strcpy(ptr[*nrOfSongs].year, line);
counter = 1;
*nrOfSongs += 1;
}
}
fclose(fileOpen);
}
I can now read my text-file and store my songs in my stong struct and I have allocated memory for it.
My problems occure when I'm trying to use the showList function. I can print out the ptr[i].artist and ptr[i].title correct, but for some reason when I print the ptr[i].year it show the year + the title of the next song in the list.
My text-file looks like this and each row is ended with a '\n'.
4
Mr Tambourine Man
Bob Dylan
1965
Dead Ringer for Love
Meat Loaf
1981
Euphoria
Loreen
2012
Love Me Now
John Legend
2016
I can't understand why it prints out more than the year. I noticed that if I make the year array in my struct the same size as the other 2, the problem goes away. But I want to understand.
When I debugg in VS it says that it's stored the right values in the year arr.
Anyone know what I might have missed?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SLENG 50 //just a random value
typedef struct Song
{
char *name;
char *nameSong;
char *timeSong;
int date;
} Song;
void saveToFile(Song *x, int *songCount) //Saves info to the binary file
{
FILE *f = fopen("array.txt", "w");
if (f == NULL)
{
printf("Error\n");
}
fwrite(songCount, sizeof(int), 1, f);
fwrite(x, sizeof(struct Song), (*songCount), f);
fclose(f);
}
void readSong(Song *x, int *songCount) //Reads info fromt he file and writes it
{
FILE *fr = fopen("array.txt", "r");
if (fr == NULL)
{
printf("Error\n");
}
printf("Songs:\n");
fread(songCount, sizeof(int), 1, fr);
fread(x, sizeof(struct Song), (*songCount), fr);
for(int i=0; i < (*songCount); i++)
{
printf("%d. %s %s %s %d\n", (i+1), x[i].name, x[i].nameSong, x[i].timeSong, x[i].date);
}
fclose(fr);
}
void insertSong(Song *x, int Count) //Inserts new song into the array.
{
printf("\nInsert name of the band:\n");
x[Count].name=malloc(SLENG * sizeof(char));
scanf("%s", x[Count].name);
printf("Insert name of the song:\n");
x[Count].nameSong=malloc(SLENG * sizeof(char));
scanf("%s", x[Count].nameSong);
printf("Insert length of the song:\n");
x[Count].timeSong=malloc(SLENG * sizeof(char));
scanf("%s", x[Count].timeSong);
printf("Insert then song was created:\n");
scanf("%d", &(x[Count].date));
printf("\n");
}
main()
{
int songCount, menuOption;
Song *x=malloc(SLENG*sizeof(char)+SLENG*sizeof(char)+SLENG*sizeof(char)+sizeof(int));
printf("1. insert song\n 2. load from file\n ");
scanf("%d", &menuOption);
switch(menuOption)
{
case(1) :
printf("Insert how many songs do you want to input?\n");
scanf("%d", &songCount);
for(int i=0; i<songCount; i++)
{
insertSong(x, i);
}
saveToFile(x, &songCount);
break;
case(2) :
readSong(x, &songCount);
break;
}
}
I have an assingment to write a programm which would input some data into file and could read that data from that file, the problem is probably with fwrite or fread, couse it seems to crash everytime I try to load the and write the data from file. Any ideas why is it not working properly? And can I even do it like this as it is dynamic struct array. Thanks in advance.
In order to save the structure to a file, it must only contain scalar values, not pointers into memory objects. Modify your structure to use arrays instead of pointers:
typedef struct Song {
char name[SLENG];
char nameSong[SLENG];
char timeSong[SLENG];
int date;
} Song;
And modify the code accordingly, but note that:
saving and reading the structures to and from a file requires opening it in binary mode "wb" and "rb".
it is very misleading to name a binary file array.txt.
you do not need to pass the address of the count when writing to the file, but you need to pass the address of the array pointer when reading as you do not know yet how much memory to allocate.
Here is the modified code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SLENG 50 // this value is used in the file format
typedef struct Song {
char name[SLENG];
char nameSong[SLENG];
char timeSong[SLENG];
int date;
} Song;
int saveToFile(Song *x, int songCount) { //Saves info to the binary file
FILE *f = fopen("array.bin", "wb");
if (f == NULL) {
printf("Error\n");
return -1;
}
fwrite(songCount, sizeof(int), 1, f);
int written = fwrite(x, sizeof(struct Song), songCount, f);
fclose(f);
return written;
}
int readSong(Song **x, int *songCount) { //Reads info from the file and writes it
int count = 0;
FILE *fr = fopen("array.bin", "rb");
if (fr == NULL) {
printf("Error\n");
return -1;
}
printf("Songs:\n");
fread(&count, sizeof(int), 1, fr);
*x = calloc(count, sizeof(Song));
if (*x == NULL) {
printf("Cannot allocate %d bytes of memory\n", count);
fclose(fr);
return -1;
}
int found = fread(*x, sizeof(struct Song), count, fr);
for (int i = 0; i < found; i++) {
printf("%d. %s %s %s %d\n", i + 1,
(*x)[i].name, (*x)[i].nameSong, (*x)[i].timeSong, (*x)[i].date);
}
fclose(fr);
return *songCount = found;
}
void insertSong(Song *x, int Count) { //Inserts new song into the array.
printf("\nInsert name of the band:\n");
scanf("%49s", x[Count].name);
printf("Insert name of the song:\n");
scanf("%49s", x[Count].nameSong);
printf("Insert length of the song:\n");
scanf("%49s", x[Count].timeSong);
printf("Insert then song was created:\n");
scanf("%d", &(x[Count].date));
printf("\n");
}
int main(void) {
int songCount, menuOption;
Song *x = NULL;
printf("1. insert song\n 2. load from file\n ");
scanf("%d", &menuOption);
switch (menuOption) {
case 1:
printf("Insert how many songs do you want to input?\n");
if (scanf("%d", &songCount) == 1) {
x = calloc(songCount, sizeof(Song));
for (int i = 0; i < songCount; i++) {
insertSong(x, i);
}
saveToFile(x, songCount);
}
break;
case 2:
readSong(&x, &songCount);
break;
}
free(x);
x = NULL;
return 0;
}
guys! So I have an assignment to create a structure that includes variables based on what information I need to store and to bring out a menu that calls different functions and does different things. The problems is that for some reason my createFile function is not working at all and I've been looking for errors for 2 days and I simply cannot spot where this is coming from.
Ignore the changeStud funcion as I'm still working on it. I simply want to create my file when I enter the name for it and then chose the nnumber of the function. (in my case > enter filename> enter 1> it just loops the menu function and it should create a file)
#define _CRT_SECURE_NO_WARNINGS
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
char ime[50];
char fn[10];
int ocfiz, ocmat, ocpro;
};
char filename[20];
FILE *fd;
Student ps;
void createFile() {
fd = fopen(filename, "wb");
fclose(fd);
printf("File created!\n");
}
void readStud(Student *s) {
printf("Enter data for new student");
getchar();
printf("Enter student name:");
gets_s(s->ime);
printf("\nEnter FN:");
scanf_s("%s", s->fn);
printf("\nEnter marks for Maths, Programming, Physics");
scanf_s("%d %d %d", &s->ocmat, &s->ocpro, &s->ocfiz);
getchar();
}
void addStud() {
fd = fopen(filename, "a+b");
char c;
do {
readStud(&ps);
fwrite(&ps, sizeof(ps), 1, fd);
printf("More Students? (y/n) : ");
c = getchar(); getchar();
} while (c == 'y');
fclose(fd);
}
void writeAllStud() {
fd = fopen(filename, "rb");
printf("students in file\n");
fread(&ps, sizeof(ps), 1, fd);
while (!feof(fd)) {
printf("%s fn: %s ocmat: %d ocpro: %d ocfiz: %d", ps.ime, ps.fn, ps.ocmat, ps.ocpro, ps.ocfiz);
fread(&ps, sizeof(ps), 1, fd);
}
fclose(fd);
}/*
void changeStud() {
char fn2[50];
printf("enter FN:");
gets_s(fn2);
fd = fopen(filename, "r+b");
fread(&ps, sizeof(ps), 1, fd);
while (!feof(fd)) {
if (strcmp(ps.fn, fn2)==0) {
fseek(fd, -(long) sizeof(ps), SEEK_CUR);
fwrite(&ps, sizeof(ps, 1, fd));
break;
}
}
}*/
void Avg() {
}
void exportData() {
FILE *txt;
txt = fopen("students.txt", "wt");
fd = fopen(filename, "rb");
fread(&ps, sizeof(ps), 1, fd);
while (!feof(fd)) {
fprintf(txt, "%s %s marks fiz%d mat%d prog%d\n", ps.fn, ps.ime, ps.ocfiz, ps.ocmat, ps.ocpro);
fread(&ps, sizeof(ps), 1, fd);
}
fclose(fd);
fclose(txt);
printf("students have been written to a text file.\n");
}
void main() {
printf("Enter file name:");
gets_s(filename);
int c;
do {
printf("\nMenu:\n");
printf("0 Exit\n");
printf("1 Create file\n");
printf("2 Add new student data\n");
printf("3 Change data from FN\n");
printf("4 AVG marks for maths, programming, physics\n");
printf("5 export all AVG marks to .txt file\n");
scanf("%d", &c);
switch (c) {
case 1: createFile; break;
case 2: addStud; break;
//case 3: changeStud; break;
case 4: Avg; break;
case 5: exportData; break;
}
} while (c != 0);
}
i think you should use your struct variable like this:
struct Student {
char ime[50];
char fn[10];
int ocfiz, ocmat, ocpro;
}ps;
or use
struct Student ps;
instead of just student ps,or you can decleare it in main function..And passing a struct in a functin is
void readStud( struct Student *s)() {
//your code...
}
I have a program that I am trying to write that will open a file named list.txt, this file will contain a num, a ID, and a string name on each line. This program will read list.txt file and sort ID numbers and print sorted ID with number and name to index.txt file. I wrote a program code and it's working...
Here is my list.txt
(num, ID, name)
0 3 AB
1 2 BC
2 28 DC
3 1 EF
4 13 BB
10 30 CC
11 23 FF
14 16 GG
After compiled this program sorted ID with number and name, print to index.txt and it should be:
(ID, num, name)
1 3 EF
2 1 BC
3 0 AB
13 4 BB
16 14 GG
23 11 FF
28 2 DC
30 10 CC
Here is my program code:
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <stdlib.h>
#include <string.h>
#define NUM_NUMBERS 9
typedef struct student
{
int num;
int id;
char name[100];
}end;
void update();
void Sort(student array[], int n);
void load_menu();
void add(end *e);
void search(end e);
void view(end e);
FILE *fp;
FILE *f1;
int main(int argc, char** argv)
{
load_menu();
return 0;
}
void update()
{
end st[15];
int sayi[NUM_NUMBERS], number, i=0, j=0;
fp=fopen("list.txt", "r");
if( fp == NULL )
{
printf("File is not found at add();\n");
exit(0);
}
while(!feof(fp))
{
fscanf(fp,"%d%d%s",&st[i].num,&st[i].id,st[i].name);
i++;
}
Sort(st, NUM_NUMBERS);
f1=fopen("index.txt", "w");
for(int i=0; i<NUM_NUMBERS;i++)
{
fprintf(f1, "%d %d %s\n", st[i].id, st[i].num, st[i].name);
}
}
void Sort(end array[], int n)
{
int Min;
for(int i=0; i<n-1;i++)
{
Min=i;
for(int j=i+1;j<n;j++)
{
if(array[j].id<array[Min].id)
{
Min=j;
}
}
end temp=array[i];
array[i]=array[Min];
array[Min]=temp;
}
}
void load_menu(void)
{
end e;
int choice;
do
{
printf("1. Find a record given its ID value \n");
printf("2. Add a new record to the file \n");
printf("3. View Records\n");
printf("4. Exit\n\n");
printf("Please choose one: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
search(e);
break;
case 2: add(&e);
update();
break;
case 3: view(e);
break;
case 4: printf("Done.");
return;
break;
default: printf("Invalid choice\n");
}
}while (choice != 5);
system("cls");
}
void add(end *e)
{
int i=0;
system("cls");
fp = fopen ( "list.txt", "a" );
if( fp == NULL )
{
printf("File is not found at add();\n");
exit(0);
}
printf("\n-----Add a new record-----\n");
printf("Enter number: ");
scanf("%d", &e->num);
printf("\nEnter ID : ");
scanf("%d",&e->id);
printf("\nEnter name: ");
scanf("%s",e->name);
fscanf(fp,"%d %d %s\n\n",&e->num, &e->id, e->name);
fprintf(fp,"%d %d %s\n\n",e->num ,e->id, e->name);
fclose(fp);
return;
}
void search(end e)
{
int i=0;
int sid;
system("cls");
fp = fopen ("list.txt", "r");
if(fp==NULL)
{
printf("File is not found at search();");
}
printf("\n-----Search ID-----\n");
printf("\nEnter ID : ");
scanf("%d",&sid);
printf("\nNumber ID Name");
while(!feof(fp))
{
fscanf(fp,"%d %d %s", &e.num, &e.id, &e.name);
if(sid==e.id)
{
printf("\n%d %d %s",e.num ,e.id, e.name);
}
}
printf("\n\n");
fclose(fp);
}
void view(end e)
{
int i=0;
system("cls");
printf("\n-----list.txt-----\n");
fp = fopen("list.txt", "r");
if(fp == NULL)
{
printf("File is not found at view();\n");
exit(0);
}
printf("\nNumber ID Name");
printf("\n");
while(fscanf (fp, "%d %d %s ",&e.num, &e.id, &e.name) != EOF )
printf("\n%d %d %s",e.num ,e.id, e.name);
printf("\n\n");
printf("-----index.txt-----\n");
f1 = fopen("index.txt", "r");
if(fp == NULL)
{
printf("File is not found.\n");
exit(0);
}
printf("\nNumber ID Name");
printf("\n");
while(fscanf (f1, "%d %d %s ",&e.id, &e.num, &e.name) != EOF )
printf("\n%d %d %s",e.id ,e.num, e.name);
printf("\n\n");
fclose(fp);
fclose(f1);
return;
}
But I used only array so I need to dynamically allocate at array of struct to store the info. Still don't know how to use dynamically allocate (malloc). Could you please show me how to use dynamically with code? Thanks for helps. (Sorry for bad english.)
sample code:
#include <stdio.h>
#include <stdlib.h>
typedef struct student{
int num;
int id;
char name[100];
}end;
int cmp(void const *a, void const *b){
const end *x = a;
const end *y = b;
return x->id < y->id ? -1 : x->id > y->id;
}
int main(void){
end *st = NULL, tmp;
FILE *fp;
size_t i = 0, n = 0;
fp=fopen("list.txt", "r");
if( fp == NULL ) {
perror("fopen at XXX");
exit(EXIT_FAILURE);
}
while(3 == fscanf(fp,"%d %d %s", &tmp.num, &tmp.id, tmp.name)){
end *temp = realloc(st, ++n * sizeof(*st));//Secure multiple records when the number of records is large
if(temp == NULL){
perror("realloc at XXX");
free(st);
exit(EXIT_FAILURE);
}
st = temp;
st[i++] = tmp;
}
fclose(fp);
qsort(st, n, sizeof(*st), cmp);
fp=fopen("index.txt", "w");
for(i = 0; i < n; ++i){
fprintf(fp, "%d %d %s\n", st[i].id, st[i].num, st[i].name);
}
fclose(fp);
free(st);
}
malloc() returns a pointer to the memory you allocated. The argument is the size of the memory you want to allocate, so in your case its the size of of "end".
You need to declare a pointer to "end" first and then call malloc().
end * ptr = malloc(sizeof(end));
But thats just one Element. You should definitely check out a tutorial for lists in c.
my question is as follows:
For school I have to make a little program in C to create and modify .txt files. I have gotten to create the file and write on it but when I read from it to print on the screen It doesn't do from the beginning and then the "fisical" file in my computer is all corrupted with chinese characters...
I don't know what's going on... Here's my code:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <unistd.h>
#define SIZE 1
void clrscr()
{
system("cls");
}
int getLenght(char *Rstring)
{
int n = 0;
while(Rstring[n] != '\0')
n++;
return n;
}
void getPath(char fname[])
{
printf("Insert file path: ");
gets(fname);
fflush(stdin);
}
// Returns 0 DOESNT EXIST, 1 DOES EXIST
int fileExist(char *fname)
{
if(access(fname, F_OK) != -1)
return 1;
return 0;
}
//if manage file is NULL truncate program
FILE *manageFile(char *fname)
{
if(fileExist(fname))
return fopen(fname, "r+");
else
return fopen(fname, "w+");
}
//Returns 1 if there's an ERROR 0 if NOT
int readFile(FILE *streamf, char buffer[])
{
fflush(streamf);
fseek(streamf, -1, SEEK_SET);
fread(buffer, SIZE, sizeof(buffer), streamf);
if(ferror(streamf)){
clearerr(streamf);
return 1;
}
return 0;
}
//Returns 1 if there's an ERROR 0 if NOT
int writeFile(FILE *streamf, char buffer[])
{
int NELEMT;
printf("Insert text to input file:\n");
gets(buffer);
fflush(stdin);
NELEMT = getLenght(buffer);
fflush(streamf);
fseek(streamf, 1, SEEK_END);
fwrite(buffer, SIZE, NELEMT, streamf);
if(ferror(streamf)){
clearerr(streamf);
return 1;
}
return 0;
}
int main()
{
char fname[1000];
char buffer[1000];
char choice;
int CC = 1;
FILE *streamf = NULL;
printf("\tFILE MANAGMENT SYSTEM TEST v 1.0\n");
getPath(fname);
streamf = manageFile(fname);
if(streamf == NULL){
printf("\n\nFILE OPEN ERROR! Terminate Program");
CC = 0;
}
while(CC == 1){
clrscr();
printf("\tFILE MANAGMENT SYSTEM TEST v 1.0 - MENU\nCURRENT PATH: %s\n\n", fname);
printf("1. Read File\n2. Write File\n3. Close File\n");
choice = getch();
fflush(stdin);
switch(choice)
{
case '1':
if(readFile(streamf, buffer))
printf("\n\nREAD FILE ERROR!");
else
printf("Successfully read from the file:\n%s", buffer);
getch();
break;
case '2':
if(writeFile(streamf, buffer))
printf("\n\nWRITE FILE ERROR!");
else
printf("Successful file input");
getch();
break;
case '3':
fflush(streamf);
fclose(streamf);
CC = 0;
break;
default:
printf("\n\nOPTION NOT LISTED");
getch();
}
}
return 0;
}
You're using sizeof(buffer) in the call to fread but it's not doing what you think. Look at how you pass buffer into readFile:
int readFile( FILE *streamf, char buffer[] );
This means that buffer is just a pointer, and so sizeof(buffer) will be equal to sizeof(char*). It's a classic mistake people make with sizeof so this is a rite of passage for you.
You will need to actually pass in a size to your function, or redefine it to accept char buffer[1000]. I would pass a size explicitly though:
int readFile( FILE *streamf, char buffer[], size_t buffer_size );
Also, as appeared in the comments, you are using fseek incorrectly. You need to use an offset of 0 with both SEEK_SET and SEEK_END.