473,225 Members | 1,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

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

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.

Thanks alot,
Regards,
Srikanth

Dec 22 '06 #1
9 1994

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.

Thanks alot,
Regards,
Srikanth

C strings finish in a '\0'. That means that store "hello" needs a[6],
not a[5].

Kind regards.

PS: better testDatestr[i] than &testDatestr[i][0]-

Dec 22 '06 #2

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,
I thought I'd spotted your problem... :-) But I hadn't totally.

IMHO, fscanf() is one of the warts on the face of C...
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];
I have now... How long is a date?
char testTimeStr[3][8];
And how long is a time? (Hint: How are strings represented in C? How do
we know they've finished?)
>
fp = fopen( "test.txt", "r");

while(fgetc(fp) != EOF)
OK - you've read a character here, what do you do with it?
{
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.
My initial feeling was, I think, at least part right.

I wouldn't use fscanf, it's not easy enough to use correctly. You'd do
better to read a line at a time into a buffer, then use sscanf() to
parse it. Don't forget to use the right size for your strings..

See the faq on scanf() etc for some discussion...
http://c-faq.com/stdio/index.html

Dec 22 '06 #3

tmp123 wrote:
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.

Thanks alot,
Regards,
Srikanth


C strings finish in a '\0'. That means that store "hello" needs a[6],
not a[5].
Insufficient... The program would still be broken.

Dec 22 '06 #4

srikanth wrote:
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] );
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
BTW - that doesn't compile. Please cut and paste a complete working (or
at least compiling) testcase next time. Some people won't bother adding
in the bits you've missed out.

Dec 22 '06 #5
ma**********@pobox.com wrote:
srikanth wrote:
>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] );
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

BTW - that doesn't compile. Please cut and paste a complete working
(or at least compiling) testcase next time. Some people won't bother
adding in the bits you've missed out.
And don't use // comments in usenet, and do limit line length to
something under 72 chars, 67 is a useful objective.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Dec 22 '06 #6
ma**********@pobox.com writes:
[...]
I wouldn't use fscanf, it's not easy enough to use correctly. You'd do
better to read a line at a time into a buffer, then use sscanf() to
parse it. Don't forget to use the right size for your strings..
Yes, reading a line at a time and parsing it with sscanf() is likely
to be better than using fscanf().

*But* both fscanf() and sscanf() invoke undefined behavior on numeric
overflow, so scanning numeric values is dangerous *unless* you can
somehow guarantee that nothing is going to overflow. (Overflow could
be caused by something as simple as a transmission glitch that drops
the blank between two consecutive large integers; sscanf() gives you
no way to recover from such an error, or even to detect it.) This is
IMHO a serious flaw.

One thing you can do is use sscanf() to parse the input line into
strings (using a maximum field width with each "%s"), and then use
something like strtol() to parse the string.

Or you can roll your own parsing code.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 22 '06 #7
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
Dec 23 '06 #8
Keith Thompson wrote:
>
.... snip ...
>
*But* both fscanf() and sscanf() invoke undefined behavior on
numeric overflow, so scanning numeric values is dangerous *unless*
you can somehow guarantee that nothing is going to overflow.
(Overflow could be caused by something as simple as a transmission
glitch that drops the blank between two consecutive large integers;
sscanf() gives you no way to recover from such an error, or even to
detect it.) This is IMHO a serious flaw.

One thing you can do is use sscanf() to parse the input line into
strings (using a maximum field width with each "%s"), and then use
something like strtol() to parse the string.

Or you can roll your own parsing code.
See my post in "if string is a number".

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Dec 23 '06 #9
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
/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>

#define FILE_NAME "test.txt"
#define SPACE " \t\r\n\f\v"
#define NMEMB(A) (sizeof (A) / sizeof *(A))

int
get_delimeters(char **lineptr, size_t *n, char *s, FILE *stream);

int main(void)
{
size_t n;
int rc;
char *field[] = {"num","date","time"};
size_t buff_size = 0;
char *buff_ptr = NULL;
char *fn = FILE_NAME;
FILE *fp = fopen(fn, "r");

if (fp == NULL) {
printf("fopen problem with %s.\n", fn);
exit(EXIT_SUCCESS);
}
printf("%s is open for reading.\n\n", fn);
do {
for (n = 0; n != NMEMB(field); ++n) {
do {
rc = get_delimeters
(&buff_ptr, &buff_size, "\n" SPACE, fp);
} while (rc == 1);
if (1 rc) {
break;
}
printf("%s: %s ", field[n], buff_ptr);
}
putchar('\n');
} while (rc 0);
if (rc == 0) {
puts("realloc problem in get_delimeters.");
}
free(buff_ptr);
puts("buff_ptr has been freed.");
fclose(fp);
printf("%s is closed.\n", fn);
return 0;
}

int get_delimeters(char **lineptr, size_t *n, char *s, FILE *stream)
{
int rc;
void *p;
size_t count;

count = 0;
while ((rc = getc(stream)) != EOF) {
++count;
if (count + 2 *n) {
p = realloc(*lineptr, count + 2);
if (p == NULL) {
if (*n count) {
(*lineptr)[count] = '\0';
(*lineptr)[count - 1] = (char)rc;
} else {
if (*n == 1) {
**lineptr = '\0';
}
ungetc(rc, stream);
}
count = 0;
break;
}
*lineptr = p;
*n = count + 2;
}
if (strchr(s, rc) != NULL) {
(*lineptr)[count - 1] = '\0';
break;
}
(*lineptr)[count - 1] = (char)rc;
}
if (rc != EOF) {
rc = count INT_MAX ? INT_MAX : count;
} else {
if (*n count) {
(*lineptr)[count] = '\0';
}
}
return rc;
}
/* END new.c */
--
pete
Jan 4 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: deko | last post by:
I have a text file ("eighty.txt") that looks like this: 83|84|85|86 I can read the file into an array like this: $numbers= file("eighty.txt"); But how do I key the array? I'd like to use...
1
by: Jonas | last post by:
I want the user to be able to write like http://www.google.com in one textBox and Google in an other textBox. Than I want the user to press a button when done. The text will then be past like...
11
by: Jim | last post by:
Hi, I keep getting form results emailed to me that would indicate a form from my web site is getting submitted with all fields blank or empty, but my code should preventing users from proceeding...
2
by: kak3012 | last post by:
Hi, I have a text file I will read it and write out binary. The file includes 256 coloums. I use while (infile.good()) { infile.getline (buffer,2200);
4
by: pisscot | last post by:
Lind Apr 13, 6:32 am show options Newsgroups: comp.lang.c From: piss...@gmail.com (Lind) - Find messages by this author Date: 13 Apr 2005 06:32:15 -0700 Local: Wed,Apr 13 2005 6:32 am...
19
by: Mark Richards | last post by:
I've been programming for many years, but have only recently taken a deep "C" dive (bad pun, i know) and need a lot of explanation from an expert. My questions center around those mysterious...
29
by: Tuvas | last post by:
I have a function in a program that works something like this. def load_pic_data(width,heigth,inpdat, filt=TRUE): data='' total=0 tnum=0 size=100 for y in range(0,heigth): row='' for x in...
9
by: olson_ord | last post by:
Hi, My ascii file is not exactly a comma separated file. The following is a small but complete example of such a file. (This is the ISCAS circuit file format that I need to read in.) ...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.