Printf无法正确打印字符串

时间:2013-12-03 22:31:14

标签: c pointers struct

我遇到以下问题。

由于我是C的新手,我不确定我应该在google上搜索什么样的词或术语。所以我提前道歉。

我已经将程序简化了很多,以缩小到实际问题。

问题在于:

我在调用loadfile函数之前分配内存。我传递一个指向loadfile()的指针并在那里执行代码。然后我添加了一些printfs来查看代码是否按预期工作。

好吧不是:我会说我正确设置了指针,因为函数中指针的地址和实际结构数组的地址是相同的。

但是,当我想打印字符串item.name时,屏幕上会显示一些奇怪的符号。只打印该字符串的第一个字符。

任何人都可以帮助我吗?

的main.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "warehouse.h"


int main()
{
    int row=1, choice;
    Cars* item=(Cars*)malloc(sizeof(Cars));
    char inName[64];


do
{
        choice=printmenu();                                                     /** Print menu and ask for choice **/
        switch (choice)
        {
        case 10:                                                                /** choice 10 executes the loading function **/
            printf ("Enter name of file to be opened: ");
            fflush(stdin);
            scanf ("%63s", inName);

            row=3;
            item=(Cars*)realloc(item,(row*sizeof(Cars)));
            loadfile(item,inName);

            printf(" adress in main1: %d \n", &item[0].artno);
            printf(" adress in main2: %d \n", &item[1].artno);
            printf(" name1 in main: %c \n", item[0].name[0]);
            printf(" name1 in main: %s \n", item[0].name);                      /*!!! printing the string has completely unexpected behavior, see screenshot !!!*/

            break;

        case 11: break;
        }

} while(choice!=11);

getch();
return 0;
}

loadfile.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "warehouse.h"


void loadfile(Cars *load,char inName[63])
{
    char c,cartno[10]={},camount[10]={},cprice[10]={};
    FILE *in;
    int i=0,sem=0,z=0 /*rows=0*/;

    if ( (in = fopen (inName, "r")) == NULL )                               /** if file couldn't be found, return error message and return to menu **/
    {
    printf ("Can't open %s for reading.\n", inName);
    getch();
    return 0;
    }

    while ( (c = getc (in)) != EOF )                                        /** read each character in file until end of file (EOF) is read **/
    {
    if (c!=';')                                                             /** evaluate if read character is a semicolone (semicolone separates entities in data-file **/
    {
        if (sem==0)    {   cartno[z]=c;         z++;    }                    /** all characters before the first semicolone are numbers and saved to character string cartno **/
        if (sem==1)    {   (load+i)->name[z]=c; z++;    }                   /** all characters after first and before second semicolone are saved to item.name **/
        if (sem==2)    {   camount[z]=c;        z++;    }                    /** all characters after second and before third semicolone are saved to character string camount **/
        if (sem==3)    {   cprice[z]=c;         z++;    }                    /** all characters after third and before fourth semicolone are saved to character string cprice **/
        if (sem==4)                                                         /** when fourth semicolone is reached cartno, camount and cprice is transformed to integer and floats with atoi,atof **/
        {   sem=0;
            (load+i)->artno=atoi(cartno);
            (load+i)->amount=atoi(camount);
            (load+i)->price=atof(cprice);

            i++;

            memset(cartno,0,sizeof(cartno));                                /** all positions of the temp arrays are set to zero, in case of another loop **/
            memset(camount,0,sizeof(camount));
            memset(cprice,0,sizeof(cprice));

        }
    }else              {sem++;                 z=0;    }
}

printf(" adress in loadfile1: %d \n", &(load)->artno);
printf(" adress in loadfile2: %d \n", &(load+1)->artno);
printf(" name1 in loadfile: %c \n", (load)->name[0]);
printf(" name1 in loadfile: %s \n", (load)->name);                          /*!!! printing the string has completely unexpected behavior, see screenshot !!!*/

fclose (in);
printf ("File has been loaded.\n");
return 0;
}

printmenu.c

#include <stdio.h>
#include <stdlib.h>

/** This function prints the menu **/

int printmenu()
{
    int choice;

   //system("cls");

    printf("[10]\tLoad file\n");
    printf("[11]\tQuit\n\n");

    printf("Please choose: ");
    fflush(stdin);
    scanf("%d",&choice);

    return choice;

}

warehouse.h

#ifndef WAREHOUSE_H_INCLUDED
#define WAREHOUSE_H_INCLUDED

struct cars {
    int amount, artno;
    float price;
    char name[20];

};

typedef struct cars Cars;

int printmenu();
void loadfile(Cars *load,char inName[63]);

#endif // WAREHOUSE_H_INCLUDED

这里是我正在加载的文件:

1;Bier;11;12.00;
2;Wodka;43;13.89;
3;Rum;6;29.10;
不幸的是,我不允许上传屏幕喷射,所以我不得不使用外部主机。 http://www.imagebanana.com/view/1zisbkuf/strange.jpg

printf(&#34; main1中的name1:%s \ n&#34;,item [0] .name);

我希望打印printf:main1中的name1:bier 但它打印出奇怪的迹象。

1 个答案:

答案 0 :(得分:0)

您的文件阅读功能可以轻松很多。 变化

while ( (c = getc (in)) != EOF )
{
    ...                                     
}

在loadfile.c中

char buf[256];
while (fgets(buf, 255, in)) {
    char *pch = strtok(buf, ";");
    load[i].artno=atoi(pch);

    pch = strtok(NULL, ";");
    strcpy(load[i].name, pch);

    pch = strtok(NULL, ";");
    load[i].amount = atoi(pch);

    pch = strtok(NULL, ";");
    load[i].price = atof(pch);

    i++;
}

假设加载文件的格式正确,并且每个条目都在一个新行中。