Hi again .. and thank you for your last answer to my question with the size of an array. Now I want to show you what I want to do with this array, but seems I still don't get it why I can't assign the size of the array
Here the size is known and is very easy to work with:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define END_SELECTED 6 // last highlighted choise selected
#define ARRAYSIZE(sel) (sizeof(sel) / sizeof(sel[0]))
#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_ENTER 13
void hideCursor();
void changeUser();
void SelChangeUser(unsigned int select);
void newUser();
void oldUser();
void selOldUser(unsigned int select);
void deleteUser();
main()
{
hideCursor();
changeUser();
return (0);
}
void hideCursor()
{
HANDLE cursorHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO info;
info.dwSize = 100;
info.bVisible = FALSE;
SetConsoleCursorInfo(cursorHandle, &info);
}
void changeUser()
{
static int select = 0;
int x;
SelChangeUser(select);
while((x = _getch()))
{
switch(x)
{
case KEY_UP:
{
select -= 2;
if(select < 0)
select = 0;
SelChangeUser(select);
}break;
case KEY_DOWN:
{
select += 2;
if(select > END_SELECTED)
select = END_SELECTED;
SelChangeUser(select);
}break;
case KEY_ENTER:
{
switch(select)
{
case 0:
{
newUser();
SelChangeUser(select);
}break;
case 2:
{
oldUser();
SelChangeUser(select);
}break;
case 4:
{
deleteUser();
SelChangeUser(select);
}break;
case 6:
{
printf("\n\n\n\n\t\t\t\tExit Program");
exit(0);
}break;
}
}break;
}
}
}
void SelChangeUser(unsigned int line)
{
char *selector[] = {
"\t New user",
"\t New user",
"\t Old user",
"\t Old user",
"\tDelete user",
"\tDelete user",
"\t Exit",
"\t Exit"
};
unsigned int i;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
system("cls");
printf("\n\t\t\t\tInteractive Menu\n");
printf("\t\t\t\t-------------\n\n\n");
printf("\tChoose an option:\n\n\n\n\n");
for(i = 0; i < ARRAYSIZE(selector); i += 2)
{
if(i == select)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
printf("%s\n\n", selector[i]);
}
else
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
printf("%s\n\n", selector[i + 1]);
}
}
}
void newUser()
{...
}
void oldUser()
{...
}
void deleteUser()
{...
}
Okay, now when program runs if I select old user I will access a file text called oldUser.txt where name of users will be saved like this:
oldUser.txt
Alex
Alex
Frank
Frank
Gabriel
Gabriel
because I want my array of pointers to be exactly like this:
char *selector[] =
{
"\t New user",
"\t New user",
"\t Old user",
"\t Old user",
"\tDelete user",
"\tDelete user",
"\t Exit",
"\t Exit"
};
Now oldUser() and selOldUser() functions look like this:
void oldUser()
{
int last_selected = 0;
FILE *file = fopen("newUser.txt", "r");
FILE *file2 = fopen("oldUser.txt", "a+");
char buf[MAX] = {};
for(int i = 0; fgets(buf, sizeof buf, file) != NULL; i++)
{
fprintf(file2, "%s", buf);
fprintf(file2, "%s", buf);
}
fclose(file);
rewind(file2);
for(last_selected = 0; fgets(buf, sizeof buf, file2) != NULL; ++last_selected);
fclose(file2);
remove("oldUser.txt");
static int select = 0;
unsigned int x;
selOldUser(select);
while((x = _getch()))
{
switch(x)
{
case KEY_UP: {
select -= 2;
if(select < 0)
select = 0;
selOldUser(select);
}break;
case KEY_DOWN: {
select += 2;
if(select > (last_selected - 2))
select = (last_selected - 2);
selOldUser(select);
}break;
case KEY_ENTER: {
}break;
}
}
}
selOldUser(unsigned int select)
{
FILE *file = fopen("newUser.txt", "r"); FILE *file2 = fopen("oldUser.txt", "a+");
char buffer[256] = {};
char buf[80] = {};
int enteries = 0;
int k = 0;
for(k = 0; fgets(buffer, sizeof buffer, file) != NULL; k++)
{
fprintf(file2, "%s", buffer);
k++
fprintf(file2, "%s", buffer);
}
fclose(file);
rewind(file2);
char *selector[k];
while(enteries < k && fgets(buf, sizeof buf, file2) != NULL)
{
selector[enteries] = malloc(strlen(buf) + 1);
strcpy(selector[enteries], buf);
++enteries;
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
system("cls");
printf("\n\t\t\t\tNew type menu\n");
printf("\t\t\t\t-------------\n\n\n");
printf("\tSelect user:\n\n\n\n\n");
unsigned int i;
for(i = 0; i < ARRAYSIZE(selector); i += 2)
{
if(i == select)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
printf("\t%s\n\n", selector[i]); }
else
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
printf("\t%s\n\n", selector[i + 1]); }
}
free(*selector);
fclose(file2);
remove("oldUser.txt");
}
What I have tried:
I just assign k to size of the array, but this can't be done.