473,382 Members | 1,775 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,382 software developers and data experts.

? about reading a comma delimited file

Thanks for all the help you gave me yesterday.

here is another question.

I have a comma delimited file called redirect.txt which looks like
this

test, /test.htm
test 123,/test123.htm

I am reading these values and processing them, but it seems like the
way I am doing it is not efficient. I was hoping for pointers on how
to make this more efficient.

// testparse.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"

//#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>
//#include <ctype.h>

int main(int argc, char* argv[])
{

FILE *fp;
int i;

struct test
{
char in[100];
char out[100];
} my_test [150];
fp =fopen("c:\\Redirect.txt", "r");
if (!fp)
{
printf ("Can't open test file!\n");
return 1;
}
i=0;

while ((fscanf(fp, "%[a-z \\.] %[a-z \\.,]", &my_test[i].in)) !=
EOF)
{
fgetc(fp);
fscanf(fp, "%s", &my_test[i].out);
fgetc(fp);
printf("in %s out %s\n",my_test[i].in, my_test[i].out);
++i;
}
fclose( fp);
return 0;
}
Nov 13 '05 #1
4 13262
hi*****@att.net (Hilary Cotter) wrote in
<3b**************************@posting.google.com >:
Thanks for all the help you gave me yesterday.

here is another question.

I have a comma delimited file called redirect.txt which looks like
this

test, /test.htm
test 123,/test123.htm

I am reading these values and processing them, but it seems like the
way I am doing it is not efficient. I was hoping for pointers on how
to make this more efficient.

<SNIP>
I will not catch on the non-standard header file and some minor
flaws in your code. Instead I'll do a sketch for an algorithm:

- read the file char-by-char, checking for EOF
- skip any leading whitespace
- copy characters to your 1st buffer till you hit ','
- skip the ',' and any following whitespace
- copy characters to your 2nd buffer till you hit '\n'
or whitespace
- continue till EOF

And, of course, make sure you're not producing any buffer overflows -
consider dynamical memory (re)allocation for your buffers.
Problems with implementing this? Don't hesitate to ask.

Regards

Irrwahn
--
Sig. Sic.
Nov 13 '05 #2
j

"Hilary Cotter" <hi*****@att.net> wrote in message
news:3b**************************@posting.google.c om...
Thanks for all the help you gave me yesterday.

here is another question.

I have a comma delimited file called redirect.txt which looks like
this

test, /test.htm
test 123,/test123.htm

I am reading these values and processing them, but it seems like the
way I am doing it is not efficient. I was hoping for pointers on how
to make this more efficient.

// testparse.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"

//#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>
//#include <ctype.h>

int main(int argc, char* argv[])
{

FILE *fp;
int i;

struct test
{
char in[100];
char out[100];
} my_test [150];
fp =fopen("c:\\Redirect.txt", "r");
if (!fp)
{
printf ("Can't open test file!\n");
return 1;
}
i=0;

while ((fscanf(fp, "%[a-z \\.] %[a-z \\.,]", &my_test[i].in)) !=
EOF)
{
fgetc(fp);
fscanf(fp, "%s", &my_test[i].out);
fgetc(fp);
printf("in %s out %s\n",my_test[i].in, my_test[i].out);
++i;
}
fclose( fp);
return 0;
}


Why not instead read the entire file into memory(fseek & ftell to get file
size, then malloc that size+1, then fread) and then tokenize(strtok) using
the delimiter "\n". Then further split up each word (using delimiter ','
with strchr)based on the current string you are operating on that was
returned from strtok.

Although, I am not sure if this is the most efficient way.

Oh and, you might want to use ``indent -kr -nut'' next time you post your
code(if you have a copy of indent) :)
Nov 13 '05 #3


Hilary Cotter wrote:
Thanks for all the help you gave me yesterday.

here is another question.

I have a comma delimited file called redirect.txt which looks like
this

test, /test.htm
test 123,/test123.htm

I am reading these values and processing them, but it seems like the
way I am doing it is not efficient. I was hoping for pointers on how
to make this more efficient.

// testparse.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"

//#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>
//#include <ctype.h>

int main(int argc, char* argv[])
{

FILE *fp;
int i;

struct test
{
char in[100];
char out[100];
} my_test [150];
fp =fopen("c:\\Redirect.txt", "r");
if (!fp)
{
printf ("Can't open test file!\n");
return 1;
}
i=0;

while ((fscanf(fp, "%[a-z \\.] %[a-z \\.,]", &my_test[i].in)) !=
EOF)
You might try. using the format string "%99[^,],%99s".
Another possibility is using function strtok.
{
fgetc(fp);
fscanf(fp, "%s", &my_test[i].out);
fgetc(fp);
printf("in %s out %s\n",my_test[i].in, my_test[i].out);
++i;
}
fclose( fp);
return 0;
}


If the file's data is formatted as you describe with each line
containing the "in" data and the "out" data then you could use
function fgets and function sscanf.

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

int main(int argc, char* argv[])
{
FILE *fp;
int i,count;
char buf[100], *s;
struct test
{
char in[100];
char out[100];
}my_test [150];

fp = fopen("c:\\Redirect.txt","r");
if (!fp)
{
printf ("Can't open test file!\n");
return 1;
}
for(count = 0;NULL != fgets(buf, sizeof buf, fp);count++)
{
if((s = strchr(buf,'\n')) != NULL) *s = '\0';
else {
puts("File format error");
return 1;
}
if(2 != sscanf(buf,"%99[^,],%99s",my_test[count].in,
my_test[count].out))
{
puts("File format error");
return 1;
}
}
fclose( fp);
/* Testing */
for(i = 0; i < count; i++)
printf("my_test[%d].in = %s\n"
"my_test[%d].out = %s\n\n",
i,my_test[i].in,i,my_test[i].out);
return 0;
}

--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #4
On Thu, 04 Sep 2003 12:17:14 -0400, Al Bowers
<xa*@abowers.combase.com> wrote:
Hilary Cotter wrote: <snip>
while ((fscanf(fp, "%[a-z \\.] %[a-z \\.,]", &my_test[i].in)) !=
EOF)

The range format a-z (rather than abcdef etc.) is nonstandard.
You might try. using the format string "%99[^,],%99s".
While the length limit is certainly an improvement and the simpler
complement class probably is (though you might want [^,\n] in case the
input contains any misformatted line(s), both of these contain two
conversions and one variable. You should either do one conversion
here and the other in the body of the loop below, or both here, using
%*c to skip instead of fgetc(), and none in the body.
Another possibility is using function strtok.
On lines read with fgets(), presumably, and copy the results with
strcpy() plus overflow checks, or alternative like zero + strncat().
{
fgetc(fp);
fscanf(fp, "%s", &my_test[i].out);
This doesn't allow whitespace within the second value; did you want
that?
fgetc(fp);
printf("in %s out %s\n",my_test[i].in, my_test[i].out);
++i;
No protection about i overflowing the declared array size.
}
fclose( fp);
return 0;
}


If the file's data is formatted as you describe with each line
containing the "in" data and the "out" data then you could use
function fgets and function sscanf.

Or strtok() and strcpy() or variant as above.

<snip fgets then> if(2 != sscanf(buf,"%99[^,],%99s",my_test[count].in,
my_test[count].out))


This doesn't allow whitespace in second value, per above.

<snip>

- David.Thompson1 at worldnet.att.net
Nov 13 '05 #5

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

Similar topics

7
by: RFQ | last post by:
Hi, I'm struggling here to do the following with any success: I have a comma delimited file where each line in the file is something like: PNumber,3056,Contractor,XYZ Contracting,Architect,ABC...
4
by: Ron McCafferty | last post by:
How do you read comma delimited data from programs such as Excel where the data itself contains dounble quotes and commas. It used to be you could just use input. How is it fone now? The data...
1
by: John B. Lorenz | last post by:
I'm attempting to write an input routine that reads from a comma delimited file. I need to read in one record at a time, assign each field to a field array and then continue with my normal...
1
by: ungvichian | last post by:
So, right now I'm writing a program in VC++.Net with MFC, and one of the steps involves reading numeric values from a comma delimited file (like 4.56, 2.44, 3.453 etc.). The only methods I've been...
9
by: Bernie Yaeger | last post by:
Is there a way to convert or copy a .xml file to a comma delimited text file using vb .net? Thanks for any help. Bernie Yaeger
1
by: j7.henry | last post by:
I am trying to pull specific data that is in a comma delimited file into a web page. So if my comma delimited file looks like: Name,Address,Zip Fred,123 Elm,66666 Mike,23 Jump,11111 I would...
4
by: JustSomeGuy | last post by:
Hi. I have a comma delimited text file that I want to parse. I was going to use fscanf from the C library but as my app is written in C++ I thought I'd use the std io stream library... My Text...
1
by: SNarayanan | last post by:
Hi Guys I am brand new to C++ programing. I have a file with comma delimited fields. How do I write a program to read say field # 23 and print it. I did some reading in the forum but could not...
6
by: =?Utf-8?B?UmljaA==?= | last post by:
'--this code works but only reads text into one column when contains multiple cols Dim ds1x As New DataSet Dim ConStr As String = _ "Provider=Microsoft.Jet.OLEDB.4.0;Data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.