2 esd1406 ESD1406 于 2015.07.28 15:19 提问

给文件每行加入行号,用C实现 5C

函数参数是文件名指针,规定空行不编号,行号用三位数表示如001 002...

4个回答

oyljerry
oyljerry   Ds   Rxr 2015.07.28 15:23

你按行读取文件,然后对于每一行的字符串添加数字到最前面,用一个变量来计数。

 #include <stdlib.h>


#define LINE 1024

 char *ReadData(FILE *fp, char *buf)
{
    return fgets(buf, LINE, fp);
}



void main()

{

    FILE *fp;

    char *buf,*p;



    if ((fp=fopen("bpinfo","r"))==NULL)
    {

        printf("Cannot open file!\n");
        exit(0);
    }
    buf = (char *)malloc(LINE*sizeof(char));
    p = ReadData(fp, buf);  //将每行的内容读到buf中



    while (p)

    {

        DoSomething(buf, strlen(buf));  //对该行的任意操作

        p = ReadData(fp, buf);  //指针移到下一行

    }
     fclose(fp);

}
lzp_lrp
lzp_lrp   Ds   Rxr 2015.07.28 15:35

C++为文本文件的每行加行号源代码

http://download.csdn.net/download/zhlh_xt/1333546

sina_2831808769
sina_2831808769   Rxr 2015.07.28 20:25
u011191259
u011191259   2015.08.05 17:42

#include
#include
#include
#define MAX_BYTE_SIZE_IN_A_LINE (1024)

/*getline : get line from fp, return length */
size_t getline(char **pLine, size_t *length, FILE *fp)
{
bool bSysAlloc = false;
size_t block_num = 1;
if (!*pLine)
{
bSysAlloc = true;
*pLine = (char *)malloc(MAX_BYTE_SIZE_IN_A_LINE*block_num);
memset(*pLine, 0, MAX_BYTE_SIZE_IN_A_LINE*block_num);
}

*length = 0;
char c;
while ((c = fgetc(fp)) != EOF && c != '\n')
{
    if (bSysAlloc && (*length == MAX_BYTE_SIZE_IN_A_LINE*block_num))
    {
        *pLine = (char*)realloc(*pLine, MAX_BYTE_SIZE_IN_A_LINE*(++block_num));
        memset(*pLine, 0, MAX_BYTE_SIZE_IN_A_LINE*block_num);
    }

    (*pLine)[(*length)++] = c;
}

if (c == EOF)
    return (-1);

return 0;

}

int cacExp(int base, int exp)
{
int result = 1;
int theBase = 1;
while (exp)
{
if (exp & 0x01)
result = result * base;
base = base * base;
exp = exp >> 1;
}

return result;

}

size_t int2char(int iData, char *cData)
{
const size_t radix = 10;

int iTemp = iData, iSeq = 0;
char cTemp[10];
while (iTemp > 0)
{
    if (iSeq >= 10)
        break;

    cTemp[iSeq++] = iTemp%radix;
    iTemp /= radix;
}

int i = 0;
for (; i < iSeq; ++i)
{
    cData[i] = '0' + cTemp[iSeq - i - 1];
}
cData[i] = ' ';

if (iSeq == 0)
{
    iSeq = 1;
    cData[0] = '0';
    cData[1] = ' ';
}

return iSeq;

}

int addLineNo(const char *file_nm)
{
FILE *fp = fopen(file_nm, "rb");
if (!fp)
{
printf("[Error] open file \"%s\" failed.", file_nm);
return (-1);
}

char f_nm_tmp[MAX_BYTE_SIZE_IN_A_LINE];
memset(f_nm_tmp, 0, MAX_BYTE_SIZE_IN_A_LINE);
memcpy(f_nm_tmp, file_nm, strlen(file_nm));
strcat(f_nm_tmp, "tmp");
FILE *fpTemp = fopen(f_nm_tmp, "wb");
if (!fp)
{
    printf("[Error] open file \"%s\" failed.", f_nm_tmp);
    return (-1);
}

char newLine[MAX_BYTE_SIZE_IN_A_LINE + 10];
memset(newLine, 0, MAX_BYTE_SIZE_IN_A_LINE + 10);

char *pLine = NULL;
size_t size, seq_no;
int line_seq = 0;
//while (getline(&pLine, &size, fp) != (-1))
size_t ret;
do
{
    ret = getline(&pLine, &size, fp);
    if (size >= MAX_BYTE_SIZE_IN_A_LINE)
    {
        printf("[Warning] one line in this file is too long.");
    }

    seq_no = int2char(++line_seq, newLine);
    memcpy(&newLine[seq_no + 1], pLine, size);

    free(pLine);
    pLine = NULL;

    size_t sz_w = fwrite(newLine, sizeof(char), seq_no + 1 + size, fpTemp);
    printf("[Info] write %d into file.", sz_w);
}while (ret != (-1));

free(pLine);
pLine = NULL;

fclose(fp);
fclose(fpTemp);

remove(file_nm);
rename(f_nm_tmp, file_nm);

return 0;

}

至于你说的空行不编号,行号占3个字符,代码里稍微改改就好了

Csdn user default icon
上传中...
上传图片
插入图片