2016-12-11 53 views
0

我對編程比較陌生。我試圖顯示在之前基於用戶創建的文件中找到的所有數據(用戶必須在一個條目中輸入產品代碼,產品名稱和產品價格)。 我的代碼有3個選項的菜單:C - 我如何存儲(從基於用戶的輸入)結構中的數組中的各種字符串?

  1. 顯示所有將數據從文件(僅與從選項2.事實上的最後一項工作,我做節目的工作故意像,因爲我無法弄清楚如何打印文件中的所有數據)
  2. 將數據添加到該文件(工作100%)
  3. 退出(工作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; 

    } 
    } 
} 

回答

0

文件的第一行會裏面有什麼這個fprintf

fprintf(p_datos, 
     "CODIGO DE PRODUCTO\tNOMBRE DE PRODUCTO\tPRECIO DE PRODUCTO\n\n\n"); 

當你進入這一行裏面case 1,該文件將(第一行):

p_datos = fopen("datos.txt", "r"); 

so th是fscanf

fscanf(p_datos, "%llu %s %f", &datos.codigo_producto, 
      datos.nombre_producto, &datos.precio_producto); 

將嘗試從這個第一線讀取llu

CODIGO DE PRODUCTO NOMBRE DE PRODUCTO PRECIO DE PRODUCTO 

,而是因爲它會找不到llu將立即失敗,所以你不會從任何掃描文件(嘗試檢查返回值fscanf,你會看到它將是0)。


爲什麼你看到垃圾打印?

因爲,正如我所說的,fscanf失敗,所以你的程序將打印你的結構中的任何垃圾值,看到你沒有在你的代碼的開始初始化它。

嘗試初始化您的結構的成員爲零,你會看到程序打印零。


所以......試圖挽救我能想到的大多數代碼和你的邏輯,一個變通方法是創建一個字符數組稱爲從文件last_product例如和fgets,直到它結束的話,讀入緩衝區的最後一項將是文件的最後一行。

然後你printflast_product,就像這樣:

case 1: 

    p_datos = fopen("datos.txt", "r"); 
    if (p_datos == NULL) 
    { 
    exit(1); 
    } 
    if (p_datos != NULL) 
    { 
    char last_product[100]; 
    while (fgets(last_product, 100, p_datos) != NULL) 
     ; 
    printf(
     "CODIGO DE PRODUCTO\tNOMBRE DE PRODUCTO\tPRECIO DE PRODUCTO\n\n\n"); 
    printf("%s", last_product); 
    fclose(p_datos); 
    puts("\n\n"); 
    } 

我還沒有分析或測試整個代碼,但是這應該足以讓你去。

PS:如果你高興的話,而不是僅僅打印last_product喜歡我所做的,你可以從sscanf到你struct,然後打印struct內容。

相關問題