我對編程比較陌生。我試圖顯示在之前基於用戶創建的文件中找到的所有數據(用戶必須在一個條目中輸入產品代碼,產品名稱和產品價格)。 我的代碼有3個選項的菜單:C - 我如何存儲(從基於用戶的輸入)結構中的數組中的各種字符串?
- 顯示所有將數據從文件(僅與從選項2.事實上的最後一項工作,我做節目的工作故意像,因爲我無法弄清楚如何打印文件中的所有數據)
- 將數據添加到該文件(工作100%)
- 退出(工作100%)
PS:當我嘗試t o使用選項(1)重新打開程序後,它只顯示垃圾。有沒有辦法解決這個問題? PS2:抱歉我的英語語言使用不好,我不是英語。
我在這裏顯示整個代碼:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
typedef struct
{
unsigned long long codigo_producto;
char nombre_producto[60];
float precio_producto;
} datos_de_productos;
int i;
main()
{
datos_de_productos datos;
char U = 233;
char o = 162;
int x = 0;
int tam;
FILE *p_datos;
p_datos = fopen("datos.txt", "a");
fseek(p_datos, 0, SEEK_END);
tam = ftell(p_datos);
if (tam == 0)
{
p_datos = fopen("datos.txt", "w");
fprintf(p_datos,
"CODIGO DE PRODUCTO\tNOMBRE DE PRODUCTO\tPRECIO DE PRODUCTO\n\n\n");
fclose(p_datos);
}
while (x != 3)
{
system("cls");
system("color 84");
printf("****** MEN%c PRINCIPAL ******\n\n", U);
printf("(1) - Ver el %cltimo producto ingresado en esta sesi%cn\n", U, o);
puts("(2) - Agregar datos");
puts("(3) - SALIR\n");
menu_principal: printf("Por favor, ingrese la opci%cn deseada: ", o);
scanf("%i", &x);
switch (x)
{
case 1:
system("cls");
system("color 84");
p_datos = fopen("datos.txt", "r");
if (fopen == NULL)
{
exit(1);
}
if (fopen != NULL)
{
printf(
"CODIGO DE PRODUCTO\tNOMBRE DE PRODUCTO\tPRECIO DE PRODUCTO\n\n\n");
fscanf(p_datos, "%llu %s %f", &datos.codigo_producto,
datos.nombre_producto, &datos.precio_producto);
printf("%llu\t\t%s\t\t%.2f\n", datos.codigo_producto,
datos.nombre_producto, datos.precio_producto);
fclose(p_datos);
puts("\n\n");
system("pause");
system("color 0E");
}
break;
case 2:
system("cls");
puts("Se ingresaran los datos con el siguiente prototipo:\n");
puts("codigo_producto | nombre_producto | precio_producto\n");
puts("Ejemplo: '1763482 Placa_de_video 749.99'\n");
printf(
"(n%ctese que se usan guiones bajos para separar las palabras)\n\n",
o);
system("pause");
system("cls");
system("color 0E");
puts("codigo_producto | nombre_producto | precio_producto\n");
scanf("%llu %s %f", &datos.codigo_producto, datos.nombre_producto,
&datos.precio_producto);
p_datos = fopen("datos.txt", "a");
if (fopen == NULL)
{
exit(1);
}
if (fopen != NULL)
{
fprintf(p_datos, "%llu\t\t%s\t\t%.2f\n", datos.codigo_producto,
datos.nombre_producto, datos.precio_producto);
fclose(p_datos);
system("color 84");
puts("\nProducto cargado correctamente.");
system("pause");
}
break;
case 3:
system("cls");
system("color 0F");
puts("Nos\t\t\t\t\t\t\t\tby viciecal");
sleep(1);
puts("Re");
sleep(1);
puts("Vimos");
sleep(1);
puts("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t\t\tputo");
break;
default:
goto menu_principal;
break;
}
}
}