我一直在研究这个项目,因为我是GTK的新手,我编写了c代码并编译了它,它似乎运行顺利。但是,当我添加GTK部分时,输入文件中的所有内容都是相同的地址,然后是时间:
#include <gtk/gtk.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
typedef struct wa9t
{
int h ;
int m ;
int s ;
}wa9t ;
typedef struct client {
int numc;
wa9t hd;
struct client * suiv ;
}client;
typedef struct file{
struct client * premier ;
struct client * dernier ;
int nb_elt ;
}file;
file init (file * f )
{
f->premier = NULL ;
f->dernier = NULL ;
f->nb_elt = 0 ;
}
int filevide (file *f)
{
int x ;
if (f->nb_elt == 0)
{
x=1 ;
}
else
{
x=0 ;
}
return(x);
}
int nb ( file * f)
{
int x;
x = 0 ;
while (f->dernier != NULL)
{
f->dernier = f->dernier->suiv ;
x = f->nb_elt;
}
return(x);
}
void emfiler (file * f , FILE * P_FICHIER )
{
client * cour ;
int a,b,i ;
cour = malloc(sizeof(client));
cour->numc = nb(&f)+1 ;
cour->hd.h =heure() ;
cour->hd.m = minu() ;
cour->hd.s =sec() ;
cour->suiv = NULL ;
if (filevide(&f)==1)
{
f->premier = cour ;
f->dernier = cour ;
f->nb_elt = 0;
}
else
{
cour->suiv = f->dernier ;
f->dernier= cour;
}
f->nb_elt++ ;
P_FICHIER = fopen("fichier.dat", "a");
fprintf(P_FICHIER, "%d " ,f->dernier->numc);
fprintf (P_FICHIER,"%d:" ,f->dernier->hd.h);
fprintf (P_FICHIER,"%d:" ,f->dernier->hd.m);
fprintf (P_FICHIER,"%d;\n",f->dernier->hd.s);
fclose(P_FICHIER);
system("sort /+1 fichier.dat /o fichiertrier.dat");
}
char * strdel(char * s, int pos, int n)
{
memmove(s + pos, s + pos + n, strlen(s) - pos - n + 1);
return (s);
}
int heure()
{
time_t rawtime;
struct tm * timeinfo;
char h[2];
int hh;
time ( &rawtime );
timeinfo = asctime(localtime ( &rawtime ));
strdel(timeinfo,0,10);
strdel(timeinfo,8,5);
strncpy ( h, timeinfo, 3 );
hh = atoi(h);
strdel(timeinfo,0,4);
return(hh);
}
int minu()
{
time_t rawtime;
struct tm * timeinfo;
char m[2];
int mm;
time ( &rawtime );
timeinfo = asctime(localtime ( &rawtime ));
strdel(timeinfo,0,10);
strdel(timeinfo,8,5);
strdel(timeinfo,0,4);
strncpy ( m, timeinfo, 3 );
mm = atoi(m);
return(mm);
}
int sec()
{
time_t rawtime;
struct tm * timeinfo;
char s[2];
int ss;
time ( &rawtime );
timeinfo = asctime(localtime ( &rawtime ));
strdel(timeinfo,0,10);
strdel(timeinfo,8,5);
strdel(timeinfo,0,4);
strdel(timeinfo,0,3);
ss = atoi(timeinfo);
return(ss);
}
void MessageEmfiler (file * f , FILE * P_FICHIER)
{
if (((heure() > 18)||(heure() < 8)))
{
GtkWidget *dialog, *label, *content_area;
GtkDialogFlags flags;
GtkWindow *window;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Gestion des tickets");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 50);
// Create the widgets
flags = GTK_DIALOG_DESTROY_WITH_PARENT;
dialog = gtk_dialog_new_with_buttons ("Message",window,flags,("_OK"),GTK_RESPONSE_NONE,NULL);
content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
label = gtk_label_new ("Desole, mais sakarna saye");
// Ensure that the dialog box is destroyed when the user responds
g_signal_connect_swapped (dialog,"response",G_CALLBACK (gtk_widget_destroy),dialog);
// Add the label, and show everything we’ve added
printf("\a");
gtk_container_add (GTK_CONTAINER (content_area), label);
gtk_widget_show_all (dialog);
}
else
{
emfiler (&f,&P_FICHIER );
printf("\a");
}
}
int main (int argc,char *argv[])
{
file *f ;
FILE * P_FICHIER;
init (&f) ;
GtkWindow *window;
GtkWidget *grid;
GtkWidget *button;
system("color a");
system("title GTK");
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Gestion des tickets");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 50);
printf("%d",nb(&f));
printf("\n");
grid = gtk_grid_new ();
gtk_container_add (GTK_CONTAINER (window), grid);
button = gtk_button_new_with_label ("Obtenir votre ticket ");
g_signal_connect (button, "clicked", G_CALLBACK (MessageEmfiler), (&f,&P_FICHIER));
gtk_grid_attach (GTK_GRID (grid), button, 1, 0, 1, 1);
button = gtk_button_new_with_label ("Quitter");
g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL);
gtk_grid_attach (GTK_GRID (grid), button, 0, 1, 2, 1);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
答案 0 :(得分:0)
在函数emfiler中,您正在发送指针的地址:
cour->numc = nb(&f)+1 ;
你可以尝试
cour->numc = nb(f)+1;
编辑:
让我指出一些可能有用的事情。
你的init函数假设指针被分配了一个内存。我相信你实际上想要在init函数的开头malloc它,因为这似乎是在main中调用init(&amp; f)的意图。当然,您需要更新调用者的指针值,因此您应该将指针传递给指针(如果您只是传递指针,则在函数调用时复制该指针并且调用者指针不会更改)。所以函数看起来应该是这样的:
void init(file **f)
{
*f = malloc(sizeof(struct file));
*f->premier = NULL;
*f->dernier = NULL:
*f->nb_elt = 0;
}
答案 1 :(得分:0)
在此处发布,因为它不符合评论。
首先,minimal example会更有用......特别适合你,因为我很确定你自己能找到问题。
然后,您必须不忽略编译器警告,尤其是当您发现某些意外行为时。你有31个警告!以下是快速编译的结果:
a.c: In function ‘emfiler’:
a.c:63:8: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration]
cour = malloc(sizeof(client));
^~~~~~
a.c:63:8: warning: incompatible implicit declaration of built-in function ‘malloc’
a.c:63:8: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’
a.c:64:17: warning: passing argument 1 of ‘nb’ from incompatible pointer type [-Wincompatible-pointer-types]
cour->numc = nb(&f)+1 ;
^
a.c:47:5: note: expected ‘file * {aka struct file *}’ but argument is of type ‘file ** {aka struct file **}’
int nb ( file * f)
^~
a.c:65:13: warning: implicit declaration of function ‘heure’ [-Wimplicit-function-declaration]
cour->hd.h =heure() ;
^~~~~
a.c:66:14: warning: implicit declaration of function ‘minu’ [-Wimplicit-function-declaration]
cour->hd.m = minu() ;
^~~~
a.c:67:13: warning: implicit declaration of function ‘sec’ [-Wimplicit-function-declaration]
cour->hd.s =sec() ;
^~~
a.c:69:14: warning: passing argument 1 of ‘filevide’ from incompatible pointer type [-Wincompatible-pointer-types]
if (filevide(&f)==1)
^
a.c:33:5: note: expected ‘file * {aka struct file *}’ but argument is of type ‘file ** {aka struct file **}’
int filevide (file *f)
^~~~~~~~
a.c:89:1: warning: implicit declaration of function ‘system’ [-Wimplicit-function-declaration]
system("sort /+1 fichier.dat /o fichiertrier.dat");
^~~~~~
a.c: In function ‘heure’:
a.c:106:12: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
timeinfo = asctime(localtime ( &rawtime ));
^
a.c:107:10: warning: passing argument 1 of ‘strdel’ from incompatible pointer type [-Wincompatible-pointer-types]
strdel(timeinfo,0,10);
^~~~~~~~
a.c:93:8: note: expected ‘char *’ but argument is of type ‘struct tm *’
char * strdel(char * s, int pos, int n)
^~~~~~
a.c:108:10: warning: passing argument 1 of ‘strdel’ from incompatible pointer type [-Wincompatible-pointer-types]
strdel(timeinfo,8,5);
^~~~~~~~
a.c:93:8: note: expected ‘char *’ but argument is of type ‘struct tm *’
char * strdel(char * s, int pos, int n)
^~~~~~
a.c:109:16: warning: passing argument 2 of ‘strncpy’ from incompatible pointer type [-Wincompatible-pointer-types]
strncpy ( h, timeinfo, 3 );
^~~~~~~~
In file included from a.c:4:0:
/usr/include/string.h:129:14: note: expected ‘const char * restrict’ but argument is of type ‘struct tm *’
extern char *strncpy (char *__restrict __dest,
^~~~~~~
a.c:110:8: warning: implicit declaration of function ‘atoi’ [-Wimplicit-function-declaration]
hh = atoi(h);
^~~~
a.c:111:10: warning: passing argument 1 of ‘strdel’ from incompatible pointer type [-Wincompatible-pointer-types]
strdel(timeinfo,0,4);
^~~~~~~~
a.c:93:8: note: expected ‘char *’ but argument is of type ‘struct tm *’
char * strdel(char * s, int pos, int n)
^~~~~~
a.c: In function ‘minu’:
a.c:123:12: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
timeinfo = asctime(localtime ( &rawtime ));
^
a.c:125:10: warning: passing argument 1 of ‘strdel’ from incompatible pointer type [-Wincompatible-pointer-types]
strdel(timeinfo,0,10);
^~~~~~~~
a.c:93:8: note: expected ‘char *’ but argument is of type ‘struct tm *’
char * strdel(char * s, int pos, int n)
^~~~~~
a.c:126:10: warning: passing argument 1 of ‘strdel’ from incompatible pointer type [-Wincompatible-pointer-types]
strdel(timeinfo,8,5);
^~~~~~~~
a.c:93:8: note: expected ‘char *’ but argument is of type ‘struct tm *’
char * strdel(char * s, int pos, int n)
^~~~~~
a.c:127:10: warning: passing argument 1 of ‘strdel’ from incompatible pointer type [-Wincompatible-pointer-types]
strdel(timeinfo,0,4);
^~~~~~~~
a.c:93:8: note: expected ‘char *’ but argument is of type ‘struct tm *’
char * strdel(char * s, int pos, int n)
^~~~~~
a.c:128:16: warning: passing argument 2 of ‘strncpy’ from incompatible pointer type [-Wincompatible-pointer-types]
strncpy ( m, timeinfo, 3 );
^~~~~~~~
In file included from a.c:4:0:
/usr/include/string.h:129:14: note: expected ‘const char * restrict’ but argument is of type ‘struct tm *’
extern char *strncpy (char *__restrict __dest,
^~~~~~~
a.c: In function ‘sec’:
a.c:141:12: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
timeinfo = asctime(localtime ( &rawtime ));
^
a.c:143:10: warning: passing argument 1 of ‘strdel’ from incompatible pointer type [-Wincompatible-pointer-types]
strdel(timeinfo,0,10);
^~~~~~~~
a.c:93:8: note: expected ‘char *’ but argument is of type ‘struct tm *’
char * strdel(char * s, int pos, int n)
^~~~~~
a.c:144:10: warning: passing argument 1 of ‘strdel’ from incompatible pointer type [-Wincompatible-pointer-types]
strdel(timeinfo,8,5);
^~~~~~~~
a.c:93:8: note: expected ‘char *’ but argument is of type ‘struct tm *’
char * strdel(char * s, int pos, int n)
^~~~~~
a.c:145:10: warning: passing argument 1 of ‘strdel’ from incompatible pointer type [-Wincompatible-pointer-types]
strdel(timeinfo,0,4);
^~~~~~~~
a.c:93:8: note: expected ‘char *’ but argument is of type ‘struct tm *’
char * strdel(char * s, int pos, int n)
^~~~~~
a.c:146:10: warning: passing argument 1 of ‘strdel’ from incompatible pointer type [-Wincompatible-pointer-types]
strdel(timeinfo,0,3);
^~~~~~~~
a.c:93:8: note: expected ‘char *’ but argument is of type ‘struct tm *’
char * strdel(char * s, int pos, int n)
^~~~~~
a.c: In function ‘MessageEmfiler’:
a.c:159:9: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
^
a.c:178:13: warning: passing argument 1 of ‘emfiler’ from incompatible pointer type [-Wincompatible-pointer-types]
emfiler (&f,&P_FICHIER );
^
a.c:59:6: note: expected ‘file * {aka struct file *}’ but argument is of type ‘file ** {aka struct file **}’
void emfiler (file * f , FILE * P_FICHIER )
^~~~~~~
a.c:178:16: warning: passing argument 2 of ‘emfiler’ from incompatible pointer type [-Wincompatible-pointer-types]
emfiler (&f,&P_FICHIER );
^
a.c:59:6: note: expected ‘FILE * {aka struct _IO_FILE *}’ but argument is of type ‘FILE ** {aka struct _IO_FILE **}’
void emfiler (file * f , FILE * P_FICHIER )
^~~~~~~
a.c: In function ‘main’:
a.c:188:12: warning: passing argument 1 of ‘init’ from incompatible pointer type [-Wincompatible-pointer-types]
init (&f) ;
^
a.c:24:6: note: expected ‘file * {aka struct file *}’ but argument is of type ‘file ** {aka struct file **}’
file init (file * f )
^~~~
a.c:195:12: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
^
a.c:199:20: warning: passing argument 1 of ‘nb’ from incompatible pointer type [-Wincompatible-pointer-types]
printf("%d",nb(&f));
^
a.c:47:5: note: expected ‘file * {aka struct file *}’ but argument is of type ‘file ** {aka struct file **}’
int nb ( file * f)
^~
a.c:209:26: warning: passing argument 1 of ‘gtk_widget_show_all’ from incompatible pointer type [-Wincompatible-pointer-types]
gtk_widget_show_all (window);
^~~~~~
In file included from /usr/include/gtk-3.0/gtk/gtkapplication.h:27:0,
from /usr/include/gtk-3.0/gtk/gtkwindow.h:33,
from /usr/include/gtk-3.0/gtk/gtkdialog.h:32,
from /usr/include/gtk-3.0/gtk/gtkaboutdialog.h:30,
from /usr/include/gtk-3.0/gtk/gtk.h:31,
from a.c:1:
/usr/include/gtk-3.0/gtk/gtkwidget.h:632:12: note: expected ‘GtkWidget * {aka struct _GtkWidget *}’ but argument is of type ‘GtkWindow * {aka struct _GtkWindow *}’
void gtk_widget_show_all (GtkWidget *widget);