将文本转换为二进制文件 [英] Converting text to a binary file

查看:322
本文介绍了将文本转换为二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将以下格式的文本文件转换为二进制文件:

I need to convert a text file of the following format to binary:

第一行包含库存中的产品数量,以下各行包含:
产品名称'\ t'产品价格'\ t'数量'\ n'(列之间可以有多个\ t)

The first line contains the number of products in the inventory, The following lines contains:
product name '\t' product price '\t' quantity '\n' (there can be more than one \t between columns)

对于每种产品,二进制输出文件将包含一个表示产品名称长度的int,保存该产品名称的字符,一个表示价格的int和一个表示数量的int.

For every product the binary output file will contain an int representing the length of the product name, the chars that hold the product name, an int representing the price and an int representing the quantity.

样本输入文件:

Asus Zenbook    1000    10
iPhone 5        750     22
Playstation 4   1000    0

我编写了以下代码,并且我了解到我应该以纯文本格式查看字符串,而整数将显示为乱码(二进制):

I have wrote the following code, and I understood that I'm supposed to see the string in plain text while the integers will show up as gibberish (in binary):

int convertTextToBinary(char *fileName)
{
    FILE *pText, *pBinary;

    int size, i;

    char *currProdName;
    int currProdNameLen, currQuantity, currPrice;

    if (checkFileExists(fileName) == FALSE)
    {
        printf("- Given file does not exists!\n");
        return ERROR;
    }

    else
        pText = fopen(fileName, "r");

    // get the number of products in the inventory
    fscanf(pText, "%d", &size);
    #ifdef DBG
    printf("##DBG Successfuly read &size = %d DBG##\n", size);
    #endif  
    pBinary = fopen(strcat(fileName, ".bin"), "wb");

    fwrite(&size, sizeof(int), 1, pBinary);
    #ifdef DBG
    printf("##DBG Successfuly wrote &size = %d DBG##\n", size);
    #endif  
    for (i = 0; i < size; i++)
    {
        // get product name and name length
        currProdNameLen = getProdName(pText, &currProdName);
        #ifdef DBG
        printf("##DBG %d Successfuly read &currProdName = %s DBG##\n", i+1, currProdName);
        printf("##DBG %d Successfuly read &currProdNameLen = %d DBG##\n", i+1, currProdNameLen);
        #endif          
        // get product price 
        fscanf(pText, "%d", &currPrice);
        printf("##DBG %d Successfuly read &currPrice = %d DBG##\n", i+1, currPrice);
        // get product quantity
        fscanf(pText, "%d", &currQuantity);
        printf("##DBG %d Successfuly read &currQuantity = %d DBG##\n", i+1, currQuantity);
        // write data to binary file
        fwrite(&currProdNameLen , sizeof(int), 1, pBinary);
        fwrite(&currProdName, sizeof(char), currProdNameLen, pBinary);
        fwrite(&currPrice, sizeof(int), 1, pBinary);
        fwrite(&currQuantity, sizeof(int), 1, pBinary);
        free(currProdName);
    }

    fclose(pText);
    fclose(pBinary);
    return 1;
}

/* This function checks if a file in a given path exists or not by using fopen with "read" argument */
BOOL checkFileExists(char *fileName)
{
    FILE *fp;

    fp = fopen(fileName, "r");

    // file does not exists
    if (fp == NULL)
        return FALSE;

    // file does exists
    else
    {
        fclose(fp);
        return TRUE;
    }
}
int getProdName(FILE *fp, char **prodName)
{
    int nameLen = 0, offset;

    // count the length of the product name
    while (fgetc(fp) != '\t')
        nameLen++;

    // allcoate memory for the product name
    *prodName = (char*)malloc(sizeof(char)*nameLen);
    //checkalloc(&prodName);

    // get the cursor back to the original position
    offset = -1 * nameLen;
    fseek(fp, offset, SEEK_CUR);

    // copy product name from text to string
    fgets(*prodName, nameLen, fp);

    return strlen(*prodName);
}

但是,我的输出文件看起来像这样:

But the hell, my output file looks like this:

       ¨ ּּּּּט        ¨ ּּּ¯        ¨ ּּּּּּּּ   ּּּ«
        ¨      

不包含任何纯文本.我尝试将fopen参数从"wb"更改为"w",但仍然出现乱码.我在做什么错了?

Which holds no plain text. I have tried changing the fopen argument from "wb" to "w" but I still get gibberish files. What am I doing wrong?

推荐答案

在这里您编写了指针和其他垃圾,而不是它指向的字符串:

Here you write the pointer and additional garbage instead of the string it points to:

    fwrite(&currProdName, sizeof(char), currProdNameLen, pBinary);

您应该使用:

    fwrite(currProdName, sizeof(char), currProdNameLen, pBinary);

在您的版本中,您正在将指针传递给指针,但是您想传递指针本身.

In your version you are passing the pointer to the pointer, but you want to pass the pointer itself.

BTW:在您的函数 getProdName()中,您应该添加一个附加字符,因为您要分配确切的字符串长度,但在 0 字节处没有空间结束.这也会引起问题.另外, fgets 读少一个字符.检查手册页中的 fgets .除了使用 fgets ,您还可以使用 fread ,因为您仍然知道长度.无需其他解析.

BTW: In your function getProdName(), you should add an additional character, because you are allocating the exact string lenght, but no room for the 0 Byte at the end. This can also cause problems. Also fgets reads one char less. Check the man page for fgets. Instead of using fgets, you can also use fread because you know the length anyway. No additional parsing needed.

更新

更改此:

    fscanf(pText, "%d", &currQuantity);

    fscanf(pText, "%d\n", &currQuantity);

这篇关于将文本转换为二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆