Re: Is it possible to read numbers and strings of same line with..



srikanth wrote:

i have a text file like below,

test.txt file (actually my test file file is with 10000 lines but here
i tested with 3 lines)

3 06.09.2006 16:37:25
3 06.09.2006 16:40:02
3 06.09.2006 16:42:31

i want to read this and output as it looks but iam getting abnormal
results like this and i used fscanf in my code,

RESULT:

num: 3 date: .09.2006
time:16:37:2516:40:0216:42:31
num: 3 date: 06.09.200606.09.2006 time:16:40:0216:42:31
num: 3 date: 06.09.2006 time:16:42:31

my code is
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
char testDatestr[3][10];
char testTimeStr[3][8];

fp = fopen( "test.txt", "r");

while(fgetc(fp) != EOF)
{
fscanf(fp, "%d %s
%s",&SeriNumber,&testDatestr[i][0],&testTimeStr[i][0]);
i++;
}

fclose(fp);

for(j = 0; j<3; j++)
printf("\n num: %d date: %s
time:%s",SeriNumber,testDatestr[j],testTimeStr[j] );
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

could anyone can please suggest me how can i read numbers and strings
of a file and output as it is.

/* BEGIN new.c */
/*
** There are only three different values
** that can be assigned to rc
** from the fscanf calls in this program.
** They are:
** EOF
** 0
** 1
** If rc equals EOF, then the end of file was reached,
** or there is some input problem;
** and ferror and feof can be used to distinguish which.
** If rc equals 0, then an empty line was entered
** and the array contains garbage values.
** If rc equals 1, then there is a string in the array.
** Up to LENGTH number of characters are read
** from a line of a text stream
** and written to a string in an array.
** If the line is longer than LENGTH,
** then the extra characters are discarded.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define FILE_NAME "test.txt"
#define LENGTH 32
#define str(x) # x
#define xstr(x) str(x)
#define SPACE " \t\r\n\f\v"

int main(void)
{
int rc;
char array[LENGTH + 1];
FILE *fp;
char *fn, *p, *p1, *p2;
const unsigned min = sizeof "3 06.09.2006 16:42:31";
/* 01234567890123456789012345678901 */

if (min > sizeof array) {
puts("min > sizeof array");
printf("%u > %d\n", min, LENGTH + 1);
exit(EXIT_SUCCESS);
}
fn = FILE_NAME;
fp = fopen(fn, "r");
if (fp == NULL) {
printf("fopen problem with %s.\n", fn);
exit(EXIT_SUCCESS);
}
printf("%s is open.\n\n", fn);
for (;;) {
rc = fscanf(fp, "%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(fp)) {
getc(fp);
}
if (rc == 0) {
array[0] = '\0';
}
if (rc == EOF) {
break;
}
p = array;
if (strlen(p) != min - 1) {
puts("Unexpectedly short line found.\n"
"This isn't the right file format.");
break;
}
if (p[10] != '.' || p[13] != '.' ||
p[26] != ':' || p[29] != ':')
{
puts("Dots and colons not found "
"where they're supposed to be.\n"
"This isn't the right file format.");
break;
}
if (isspace(p[7]) == 0 || isspace(p[23]) == 0) {
puts("Space characters not found "
"where they're supposed to be.\n"
"This isn't the right file format.");
break;
}
if (isdigit(*p) == 0) {
puts("Frist character not a digit.\n"
"This isn't the right file format.");
break;
}
do {
++p;
} while (isdigit(*p) != 0);
*p++ = '\0';
while (isspace(*p) != 0) {
++p;
}
p1 = p++;
while (*p != '\0' && isspace(*p) == 0) {
++p;
}
*p++ = '\0';
while (isspace(*p) != 0) {
++p;
}
p2 = p;
printf("num: %s date: %s time: %s\n", array, p1, p2);
}
fclose(fp);
printf("\n%s is closed.\n", fn);
return 0;
}

/* END new.c */


--
pete
.



Relevant Pages

  • Re: Discarding unread data after scanf()
    ... A call to fscanf with stdin as the first argument, ... ** If rc equals EOF, then the end of file was reached, ... then there is a string in the array. ... ** then the extra characters are discarded. ...
    (comp.lang.c)
  • Re: Return type of "readin" and correct error handling
    ... the largest amount of allowed characters. ... ** If rc equals EOF, then the end of file was reached, ... then there is a string in the array. ...
    (comp.lang.c)
  • Re: Reading input problem
    ... ** If rc equals EOF, then the end of file was reached, ... then there is a string in the array. ... ** Up to LENGTH number of characters are read ...
    (comp.lang.c)
  • Re: ...Then what can I USE
    ... > characters); ... ** If rc equals EOF, then the end of file was reached. ... then there is a string in array. ... int main ...
    (comp.lang.c)
  • Re: sending echo to all clients
    ... I did initialize it up properly, ... There is only one array and that is an array of pollfd structures named ... as an array of characters only but then I can't because sendsends bytes ... you receive C-style strings, so there's really no point to doing it. ...
    (comp.unix.programmer)