テキストファイル1の特定の文字列から”=====”までの情報を1消しテキストファイル2に書き換えたいのですが、なかなかうまくいかないです。
名前を検索したらその人のNAME,ID,TEL,DEP,===までけして2のほうに移し替えたいです。それのID検索でNAME,ID,TEL,DEP,===をけすのも作りたいのですが、名前のほうでてこずってています。
よろしくお願いいたします。
int main() { int key; char name[100]; char str[1000] = { '\0' }; const char file_name[] = "tel.txt"; const char file_name2[] = "tel2.txt"; FILE* fp = fopen(file_name, "r"); FILE* fp2 = fopen(file_name2, "w"); printf("Do you want to search by 1.ID or 2.name?(Enter number 1 or 2): "); scanf("%d", &key); if (key = 2) { printf("enter name: "); scanf("%s",name); if (strstr(str, name) != str) { fprintf(fp2, str); fgets(str, sizeof(str), fp); fprintf(fp2, str); fgets(str, sizeof(str), fp); fprintf(fp2, str); fgets(str, sizeof(str), fp); fprintf(fp2, str); fgets(str, sizeof(str), fp); fprintf(fp2, str); } } } fclose(fp); fclose(fp2); return 0; }
tel.txt
NAME: aaa
ID: ID01
TEL: 090xxxxxxxx
DEP: a
======================
NAME: bbb
ID: ID02
TEL: 090xxxxxxxx
DEP: b
=======================
NAME: ccc
ID: ID03
TEL: 090xxxxxxxx
DEP: a
=======================
NAME: ddd
ID: ID04
TEL: 090xxxxxxxx
DEP: a
=======================
NAME: eee
ID: ID05
TEL: 090xxxxxxxx
DEP: a
=======================
tel2.txt(aaa で検索した結果)
DEP: a
=======================
NAME: bbb
ID: ID02
TEL: 090xxxxxxxx
DEP: b
=======================
NAME: ccc
ID: ID03
TEL: 090xxxxxxxx
DEP: a
=======================
NAME: ddd
ID: ID04
TEL: 090xxxxxxxx
DEP: a
=======================
NAME: eee
ID: ID05
TEL: 090xxxxxxxx
DEP: a
追記
C:\Users\pokki\source\repos\Project2\Debug\Project2.exe (process 1760) exited with code 1. To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops. Press any key to close this window . . .
'Project2.exe' (Win32): Loaded 'C:\Users\pokki\source\repos\Project2\Debug\Project2.exe'. Symbols loaded. 'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. 'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. 'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. 'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140d.dll'. 'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. The thread 0x5c44 has exited with code 0 (0x0). 'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel.appcore.dll'. 'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. 'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. 'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. 'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. 'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. 'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. The thread 0x1e18 has exited with code 1 (0x1). The thread 0x2ad4 has exited with code 1 (0x1). The thread 0x3bd0 has exited with code 1 (0x1). The program '[21896] Project2.exe' has exited with code 1 (0x1).
回答2件
あなたの回答
tips
プレビュー