0

我必须使用从文件中一次读取来构建一个包含字符、整数和浮点数的结构。我可以很好地阅读字符,但我在阅读整数时遇到了问题

CountryData ReadingFile(Directory* CountryDirectory,int SortedCountryData,char *Code,int NumberofCountries)
{
    int Location,i;  // Will store Location in array and location in bytes
    char Buff[40]; //Buffer the size of the stuct
    char Tok[25];
    CountryData Country;
    off_t offset;  //Offset value
    ssize_t count;

    Location = BinarySearch(CountryDirectory,Code,0,NumberofCountries-1); //Findlocation in array
    Location =CountryDirectory[Location].Offset; //Find location in bytes
    offset = lseek(SortedCountryData, Location, SEEK_SET); //Seek to location in file
    count = read(SortedCountryData,Buff,40); //Read file

     //Data in Buff should be Code[4],Name[25],int,float

    for(i=0;i<4;i++)//Get code
    {
    Tok[i] = Buff[i];
    }
    strncpy(Country.Code,Tok,sizeof(Country.Code));//Copy Code

    for(i=0;i<25;i++)//Get Name
    {
    Tok[i]=Buff[i+4];
    }
    strncpy(Country.Name,Tok,sizeof(Country.Name));//Copy Name

        //This is where I try to read the int
    char INTEGER[4];
    for(i=0;i<4;i++) //Read an int
    {
        INTEGER[i]=Buff[33+i];
    }
    int A =(int)INTEGER;
    printf("Tok: %d",A);

    return(Country);


}
4

4 回答 4

1

我相信@slugonamission 关于指针问题是正确的,但是如果我正确理解了这个问题,int 作为 int 存储在文件中,你处理 char 数组的原因是你被迫改变你的方式做read

为此尝试 memcpy。还要注意字节序问题。

int A;
memcpy(&A, Buff + 33, 4 * sizeof(char))
于 2013-01-27T19:05:34.513 回答
1

问题在于它INTEGER是一个数组,它相当于 C 中的一个指针。所以,当你将它转换为 an 时int,你是在转换指针,而不是值,这会给你带来疯狂的结果。解决方案?转换为int指针,然后取消引用:

int *A = (int *) INTEGER;
printf("Tok: %d",*A);

作为一种快捷方式,您可以使用一些指针算法来避免循环:

int *A = (int *) (Buff + 33);
printf("Tok: %d",*A);

如果您需要一个副本,那不会制作副本(例如,如果您打算更改int并希望保持缓冲区完整)。

于 2013-01-27T19:07:31.327 回答
0

假设 int 和 float 是“二进制”编码的(写)我会做这样的事情:

struct Data
{
    union 
    {
        char Buff[40];
        struct 
        {
            char Country_Code[4];
            char Name[25];
            int  A;  /* char A[5]; convert to int using atoi */
            float F; /* char F[6]; convert to float using atof */
        };
    };
};

CountryData ReadingFile(........)
{

    int Location,i;  // Will store Location in array and location in bytes
    CountryData Country;
    off_t offset;  //Offset value
    size_t count;

......

    Data BUFF;
    count = read(SortedCountryData,BUFF.Buff,40); //Read file

     //Data in Buff should be Code[4],Name[25],int,float

    strncpy(Country.Code,BUFF.Country_Code,sizeof(Country.Code));//Copy Code
    strncpy(Country.Name,BUFF.Name,sizeof(Country.Name));//Copy Name
    int A =BUFF.A;
    printf("Tok: %d",A);
    return(Country);
}

但是如果 int 和 float 也是字符串编码的(在文本模式下,有 5 和 6 个字符,但包括终止符),我们可以在结构中将声明更改为char A[5]and char F[6],并使用 atoi 和 atof 将其转换为 int 或 float分别。

于 2013-01-27T23:06:51.623 回答
-1
char INTEGER[4];
for(i=0;i<4;i++) //Read an int
{
    INTEGER[i]=Buff[33+i];
}
int A =(int)INTEGER;

问题与保存您的值的数组有关。数组可以在 C 中自动衰减为指针,因此这里发生的情况是,在最后一行使用时,您的字符数组首先被转换为指针。在此之后,它将被转换为一个整数(并且你的编译器应该给你一个警告)。

要解决这个问题,要么实现你自己的字符串到整数例程,要么使用atoi.

于 2013-01-27T18:57:05.060 回答