473,770 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

? 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:\\Red irect.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 13304
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.ne t> wrote in message
news:3b******** *************** ***@posting.goo gle.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.

// 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:\\Red irect.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:\\Red irect.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:\\Redi rect.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.com base.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.co mbase.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.ne t
Nov 13 '05 #5

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

Similar topics

7
7659
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 Architects,... So each line is intended to be: key1,value1,key2,value2,key3,value3... and each line is to be variable in length (although it will have to be an even number of records so that each key has a value).
4
1784
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 below contains output from Access saved as a CSV. line 1,"normal data","a double quote "" here" line 2,"a comma , here ","more normal data" Input's help documents the "" will not work.
1
6692
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 processing. I am having no luck at all finding different routines written in C to read delimited files of any kind. I have a few ideas of how I might go about this but I bet I'm re-inventing the wheel and there already exists some efficient code out...
1
1725
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 able to find to read the values all involve StreamReader, and I can't figure out how to use this. Since I'm using MFC, I'm using CFile instead. Also, none of the methods seem to come with a method for error catching. I want my program to be...
9
3640
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
2590
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 like to be able to read this data and put each row into a variable? so I could display the values in a web page where I want.
4
4811
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 file looks like: First_Name, Last_Name, ID, Date/Of/Birth<newline> depending on the host the newline is either \n or \r\n
1
1411
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 get a good feel of it. Thanks in advance.
6
22565
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 Source=C:\dir1A\;Extended Properties=""Text;HDR=No;FMT=Delimited\""" Dim conn1x As New OleDb.OleDbConnection(ConStr) Dim dax1 As New OleDbDataAdapter("Select * from testabc1x.txt", conn1x)
0
10257
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10037
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.