[英]How to use strcmp to compar names in a csv Data
我想打開一個csv文件並比較第一行的內容。 我有打開已經寫好的文件的代碼,我只是不知道如何將打開的文件中的strcmp與第一行進行比較。
打開文件
#include <stdio.h>
#include <stdlib.h>
int main(void) {
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
FILE *f;
char file_name[255];
char data[127];
printf("Which file you want to open : ");
scanf("%s",file_name);
f = fopen("C:\\projekt\\datei.csv","r");
if(f == NULL){
printf("Could not open %s\n" ,file_name);
exit(0);
}
while( fgets(data, sizeof(data), f) != 0 )
fputs(data, stdout);
return 0;
}
文件內容
"Date" "Time" "Volt" "Amp" "Wirkfaktor"
26.10.13 08:00:00 237.802 1160.7682 0.7461853792
26.10.13 08:00:01 237.658 1168.92273 0.7203561543
26.10.13 08:00:02 237.815 1158.57273 0.7344799394
26.10.13 08:00:03 237.566 1174.5682 0.6960312563
26.10.13 08:00:04 238.063 1151.67273 0.80126914
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
FILE *f;
char file_name[255];
char data[127];
printf("Which file you want to open : ");
scanf("%s",file_name);
f = fopen("C:\\projekt\\datei.csv","r");
if(f == NULL){
printf("Could not open %s\n" ,file_name);
exit(0);
}
while( fgets(data, sizeof(data), f) != NULL ){
//fputs(data, stdout);
}
int good = 1;
int i;
char heading[5][20];
if ( sscanf( data, "%19s%19s%19s%19s%19s", heading[0], heading[1], heading[2], heading[3], heading[4] ) != 5 )
{
good = 0;
}
if ( good )
{
char *expected[5] = { "\"Date\"t\"Time\"t\"Volt\"t\"Amp\"t\"Wirkfaktor\"" };
for ( i = 0; i < 5; i++ )
if ( strcmp( expected[i], heading[i] ) != 0 )
good = 0;
}
if ( !good )
{
printf( "Heading line is incorrect\n" );
exit( 0 );
}
return 0;
}
要驗證文件的第一行是否正確,請首先使用sscanf
提取五個字符串,然后使用strcmp
將字符串與預期字符串進行比較。
int good = 1;
char heading[5][20];
if ( sscanf( data, "%19s%19s%19s%19s%19s", heading[0], heading[1], heading[2], heading[3], heading[4] ) != 5 )
good = 0;
if ( good )
{
char *expected[5] = { "\"Date\"", "\"Time\"", "\"Volt\"", "\"Amp\"", "\"Wirkfaktor\"" };
for ( i = 0; i < 5; i++ )
if ( strcmp( expected[i], heading[i] ) != 0 )
good = 0;
}
if ( !good )
{
printf( "Heading line is incorrect\n" );
exit( 1 );
}
strtok
將第一行拆分為標記,然后使用strcmp
將這些標記與您希望包含的字符串進行比較。 下面列出了此代碼。 "\\"Date\\"\\t\\"Time\\"\\t\\"Volt\\"\\t\\"Amp\\"\\t\\"Wirkfaktor\\""
假定第一行用制表符分隔。 char* result = NULL;
char delims[] = {'"', ' '}; // split on spaces and double quotes
char check[] = {"Date","Time","Volt","Amp","Wirkfaktor"};
result = (char*)strtok( data, delims );
int i = 0;
while(result != NULL) {
if(strcmp(result,check[i]) {
printf("File Invalid\n");
exit(0);
}
result = (char*)strtok( NULL, delims);
i++;
}
// If we get here than the file was good.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *f;
char file_name[255];
char data[127];
char dataRef[]="26.10.13 08:00:00 237.802 1160.7682 0.7461853792";
main()
{
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
f = fopen("datei.csv","r");
if(f == NULL){
printf("Could not open %s\n" ,file_name);
exit(0);
}
while( fgets(data, sizeof(data), f) != 0 )
{
fputs(data, stdout);
if (strncmp(dataRef,data,56)==0)
{
printf(" Here's the tip's between the file chain and the stack reference chain ....");
fputs(data, stdout);
fputs(dataRef, stdout);
printf("\n");
}}}
注意:在新代碼中,我已將自動聲明移出了主體,這是由於我的vc6 compilo與該主體聲明不相稱的原因。 我想用你的想象力寫了一個簡短的解決方案。 沒關系,您當然可以理解對formatc屬性具有相同功能的strcmp或strncmp的使用。 回頭見。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.