读书人

菜-一文件容入另一文件

发布时间: 2012-04-15 18:39:21 作者: rapoo

菜求助-----一文件容入另一文件
菜大忙要1.txt中取容到st.txt中。1.txt有多少行是不定的,容如下:
;for Title of report|File name of data
;--------------------------------------------------
PART NUM|c:\record\pt.dat
SFIS NUM|c:\record\sfis.dat
STATUS|c:\record\status.dat
ERROR CODE|c:\record\error.dat
;--------------------------------------------------
第一步:取候要略有";"分的行,抓取"|"前面的字符入2.txt,每值用"|"隔,2.txt的最前面是"# ",最後不要加"|"。容是子的。
# PART NUM|SFIS NUM|STATUS|ERROR CODE
第二步:取1.txt中"|"後面的容,是文件路,取此文件容,追加到才的2.txt中。如下:
# PART NUM|SFIS NUM|STATUS|ERROR CODE
11231|123|4564|456|132231|12223

高手忙段代。感。。。。




[解决办法]
以下代码已验证:

C/C++ code
#include <afx.h>void main(){    CStdioFile mFile;    CString strTemp;    mFile.Open( "1.txt ",CFile::modeRead);    int location;    CString strResult1 = _T("# ");    CString strResult2 = _T("");    if(mFile.m_pStream != 0x00000000)  //文件不为空    {        while (mFile.ReadString(strTemp))   //逐行读取        {            if(strTemp != _T(""))            {                location = strTemp.Find(_T(";"));  //获取读到的内容中';'的位置                                if (location == -1)  //没有';'的时候再进行操作                {                                        location = strTemp.Find(_T("|"));  //获取读到的内容中'|'的位置                    if (location != -1)  // 有'|'的时候再进行操作                    {                        strResult1 += strTemp.Mid(0, location) + _T("|");                        strResult2 += strTemp.Mid(location + 1, strTemp.GetLength() - location - 1) + _T("|");                    }                }            }        }    }    strResult1 = strResult1.Mid(0, strResult1.GetLength() - 1);    strResult2 = strResult2.Mid(0, strResult2.GetLength() - 1);    mFile.Open( "2.txt ",CFile::modeCreate | CFile::modeWrite);    mFile.SeekToEnd();   //光标移到最后    mFile.WriteString(strResult1 + _T("\n") + strResult2);}
[解决办法]
楼主看下这个行不行:
C/C++ code
#include<stdio.h>#include<stdlib.h>#include<string.h>void main(){    FILE *fp1,*fp2;    char str[50][100];    char st[200];    char stt[200];    int i,j,k,t,ii,jj;    fp1 = fopen("1.txt", "r");    if(fp1 == NULL)    {        exit(1);    }    k = 0;    while(fgets(str[k++], 80, fp1) != NULL);    --k;    fclose(fp1);    memset(st, 0, sizeof(st));    memset(stt, 0, sizeof(stt));            st[0] = '#';    st[1] = ' ';    ii = 2;jj = 0;    for(i = 0; i < k; ++i)    {        if(str[i][0] == ';')        {            continue;        }        t = 0;        for(j = 0;j < strlen(str[i]); ++j)        {            if(str[i][j] == '|')            {                st[ii++] = str[i][j];                t = 1;                continue;            }            if(t==0 && str[i][j] != '\n')            {                st[ii++] = str[i][j];            }            else if(str[i][j] != '\n')            {                stt[jj++] = str[i][j];            }                    }        stt[jj++] = '|';                    }    fp2 = fopen("2.txt", "w");    if(fp2 == NULL)    {        exit(1);    }    fputs(st, fp2);    fputs(stt, fp2);    fclose(fp2);} 

读书人网 >C语言

热点推荐