所以我有一个利用openalpr的脚本,并将3个牌照写入文本文件。
文本文件以空行开头,后跟3行,每行6个字符
-imagine a newline here-
JV70BB
JV7OBB
JB7QBB
我希望每个牌照都有一个阵列,但是我有一些麻烦,想知道如何让每一行都成为一个单独的版本。
我该怎么做?
if (ptr_file) {
while ((c = getc(ptr_file)) != EOF)
//puts license plates into array here
fclose(ptr_file);
答案 0 :(得分:1)
加入车牌派对。
fgets()
最适合阅读该行。代码使用扫描集来验证条目。
char buf[100];
// Validate empty line
if (fgets(buf, sizeof buf, stdin) == NULL || buf[0] != '\n') {
Handle_Error();
}
#define LP_N (3)
char *lp[LP_N][7];
int i;
for (i = 0; i < LP_N; i++) {
if (fgets(buf, sizeof buf, stdin) == NULL) {
Handle_Error();
}
// Adjust per your license plate requirements
static const char *format = "%6[0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ]";
if (sscanf(buf, format, &lp[i]) != 1 || strlen(lp) != 6) {
Handle_Error();
}
}
BTW:在OP的代码中,c
应为int c
,以区分EOF与其他char
。
答案 1 :(得分:0)
尝试:
int i, nb_plate = 3;
FILE *ptr_file = fopen("yourFile.txt", "r+");
char array[10][50]; //you can do malloc... don't forget to free
rewind(ptr_file); //rewind
while(cget(ptr_file) != '\r'); //skip 1st line
for(i=0;i<nb_plate;i++)
fscanf(ptr_file, "%s", &yourArray[i][0]); // get one plate
答案 2 :(得分:0)
如果你想逐个字符地阅读文件(看起来很可能看你的代码),只要确保你准备好了6个字符,你应该把它放在任何地方,然后阅读换行符(一个或两个,这些可以是&#39; \ r&#39;和&#39; \ n&#39;)。你可以使用一个简单的while循环。
如果你想逐行阅读,只需读取每一行并将其保存在单独的数组中,fgets()
应该完成工作(或fscanf()
使用正确的格式字符串)。
答案 3 :(得分:0)
将fgets()
与足够长的缓冲区一起使用,并将缓冲区复制到三个数组中的每一个,具体取决于循环计数器上的模数计算:
#define MAX_LICENSE_PLATE_LENGTH 6
char license_plate_buffer[MAX_LICENSE_PLATE_LENGTH + 2] = {0};
char array_1[MAX_LICENSE_PLATE_LENGTH + 1] = {0};
char array_2[MAX_LICENSE_PLATE_LENGTH + 1] = {0};
char array_3[MAX_LICENSE_PLATE_LENGTH + 1] = {0};
unsigned int counter = 0U;
if (!ptr_file) {
fprintf(stderr, "Error: No valid file pointer\n");
exit(EXIT_FAILURE);
}
while (fgets(license_plate_buffer, MAX_LICENSE_PLATE_LENGTH + 2, ptr_file) != NULL) {
memcpy( (counter % 3 == 0) ? array_1 : ((counter % 3 == 1) ? array_2 : array_3),
license_plate_buffer,
MAX_LICENSE_PLATE_LENGTH);
counter++;
}
答案 4 :(得分:0)
考虑在循环中使用fgets()
来读取文件。它读取整个字符串而不是字符:
FILE *fp = {0};
char buf[260];//arbitrarily big buffer
char strArray[3][7];//simple array, 3 string of 7 char each (enough for NULL terminator)
i = -1;
fp = fopen (path, "r");
if(fp)
{
while (fgets (buf, 260, fp))//will continue to loop as long as there is a new line
{
if(strlen(buf) == 6) {//check that string length matches that specified in your example code
i++; //anything bigger will overflow strArray[i], anything less is not a
//valid license string (according to your current definitions)
strcpy(strArray[i], buf);
}
}
}
fclose(fp);
答案 5 :(得分:0)
much better to use fgets() to read each line of the file
if (ptr_file)
{
char licensePlate[10];
memset( licensePlate, 0x00, sizeof( licensePlate ) );
if( NULL == fgets( licensePlate, sizeof(licensePlate), ptr_file )
{
perror( "fgets failed on first line" );
exit( EXIT_FAILURE );
}
// implied else, fgets successful
// this was the leading blank line, do, do nothing
// get first plate
memset( licensePlate, 0x00, sizeof( licensePlate ) );
if( NULL == fgets( licensePlate, sizeof(licensePlate), ptr_file )
{
perror( "fgets failed" );
exit( EXIT_FAILURE );
}
// implied else, fgets successful
if( '\n' == licensePlate[ strlen(licensePlate) )
{
licensePlate[ strlen(licensePlate)] = '\0'; // chop off trailing newline
}
strcpy( plateArray_1, licensePlate );
// get second plate
memset( licensePlate, 0x00, sizeof( licensePlate ) );
if( NULL == fgets( licensePlate, sizeof(licensePlate), ptr_file )
{
perror( "fgets failed" );
exit( EXIT_FAILURE );
}
// implied else, fgets successful
if( '\n' == licensePlate[ strlen(licensePlate) )
{
licensePlate[ strlen(licensePlate)] = '\0'; // chop off trailing newline
}
strcpy( plateArray_2, licensePlate );
// get third plate
memset( licensePlate, 0x00, sizeof( licensePlate ) );
if( NULL == fgets( licensePlate, sizeof(licensePlate), ptr_file )
{
perror( "fgets failed" );
exit( EXIT_FAILURE );
}
// implied else, fgets successful
if( '\n' == licensePlate[ strlen(licensePlate) )
{
licensePlate[ strlen(licensePlate)] = '\0'; // chop off trailing newline
}
strcpy( plateArray_3, licensePlate );
fclose(ptr_file);
However, if there were more plates to read, I would make
the code from memset to strcpy into a sub routine
and pass in the ptr_file and address of the receiving array