473,783 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading strings from file

Hi

I have this problem that I can't solve, I know it shouldn't be hard but
I'm not a good coder and I'm going crazy tonight...

I have a txt file that goes like this:

string1 string2
string3 string4

I'd like to read it and store string1 and string in array1 and string2
and 4 in array2

can u please advice me the right way to do this? I'm using C/unix

thanks a lot and I wish u all a great 2006
--
there are no numbers in my email address
Jan 2 '06 #1
6 4701
Giff a écrit :
I have this problem that I can't solve, I know it shouldn't be hard
but I'm not a good coder and I'm going crazy tonight...

I have a txt file that goes like this:

string1 string2 string3 string4
Do you meant:

string1<blank>s tring2<EOL>
string3<blank>s tring4<EOL>

?
I'd like to read it and store string1 and string in array1 and
string2 and 4 in array2


You can use each line with fgets() and then extract the strings with
sscanf() and "%s %s". Make sure that sscanf() returns 2.

You can store the result in an array of strings with a fixed width (2D
array of char) or allocate the required space (malloc()) to store the
strings, and to store the addresses of the allocated blocks on an array
of pointers to char. This array could also be dynamically allocated. All
depends on the required flexibility.
--
A+

Emmanuel Delahaye
Jan 2 '06 #2
Emmanuel Delahaye ha scritto:

Do you meant: string1<blank>s tring2<EOL>
string3<blank>s tring4<EOL> ?
exactly, thanks for replying

You can use each line with fgets() and then extract the strings with
sscanf() and "%s %s". Make sure that sscanf() returns 2.


I was trying with fscanf, now I tried the way u suggested with no results:

char *array1[2], *array2[2],buff[512];
FILE *f;

i=0;
do {

if ( fgets(buff,size of(buff),f)==NU LL ) perror("fgets") ;
if ( (i = sscanf(buff,"%s %s",array1[i],array2[i])) printf("%d\n",i );
i++;
}while(i<2);

it opens the file correctly but then the sscanf returns 0, it gets stuck
during the second iteraction of the do-while

I'm sure I'm doing some really stupid mistakes, I'm not experienced,I'v e
been coding the whole day and this should be working tomorrow

thanks a lot

--

questo articolo e` stato inviato via web dal servizio gratuito
http://www.newsland.it/news segnala gli abusi ad ab***@newsland. it
Jan 2 '06 #3
At about the time of 1/2/2006 2:14 PM, Giff stated the following:
Hi

I have this problem that I can't solve, I know it shouldn't be hard but
I'm not a good coder and I'm going crazy tonight...

I have a txt file that goes like this:

string1 string2
string3 string4

I'd like to read it and store string1 and string in array1 and string2
and 4 in array2

can u please advice me the right way to do this? I'm using C/unix

thanks a lot and I wish u all a great 2006


The only what that I can think of is to read in the lines one at a time
using fgets and then using strsep to split the strings apart. Just out
of curiosity, did you write this text file or was it provided from an
outside source?

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Jan 2 '06 #4
Giff <gi*********@gm ail456.com> schrieb:
char *array1[2], *array2[2],buff[512];
FILE *f;

i=0;
do {

if ( fgets(buff,size of(buff),f)==NU LL ) perror("fgets") ;
if ( (i = sscanf(buff,"%s %s",array1[i],array2[i])) printf("%d\n",i );
Where did you allocate memory for array1 and array2?
i++;
}while(i<2);

I'm sure I'm doing some really stupid mistakes, I'm not experienced,I'v e
been coding the whole day and this should be working tomorrow


If you are using pointers, you should have a look at malloc or calloc.

Markus
Jan 2 '06 #5
The only what that I can think of is to read in the lines one at a time
using fgets and then using strsep to split the strings apart. Just out
of curiosity, did you write this text file or was it provided from an
outside source?


hi all, I made it work now! I'm not very good with array and pointers, I
mixed them up a bit

thanks a lot to everybody and again happy new year!

--

questo articolo e` stato inviato via web dal servizio gratuito
http://www.newsland.it/news segnala gli abusi ad ab***@newsland. it
Jan 2 '06 #6
At about the time of 1/2/2006 3:17 PM, Giff stated the following:
Emmanuel Delahaye ha scritto:
Do you meant:


string1<blank >string2<EOL>
string3<blank >string4<EOL>


?

exactly, thanks for replying
You can use each line with fgets() and then extract the strings with
sscanf() and "%s %s". Make sure that sscanf() returns 2.

I was trying with fscanf, now I tried the way u suggested with no results:

char *array1[2], *array2[2],buff[512];
FILE *f;

i=0;
do {

if ( fgets(buff,size of(buff),f)==NU LL ) perror("fgets") ;
if ( (i = sscanf(buff,"%s %s",array1[i],array2[i])) printf("%d\n",i );
i++;
}while(i<2);

it opens the file correctly but then the sscanf returns 0, it gets stuck
during the second iteraction of the do-while

I'm sure I'm doing some really stupid mistakes, I'm not experienced,I'v e
been coding the whole day and this should be working tomorrow

thanks a lot


I would do something like this...

#define MAX_ARRAY 2 /* number of strings to store */
#define MAX_CHAR 64 /* storage size of each string */
#define MAX_BUFF 512 /* input buffer size */

char array1[MAX_ARRAY][MAX_CHAR];
char array2[MAX_ARRAY][MAX_CHAR];
char buff[MAX_BUFF];
FILE *f;

for (i = 0; i < MAX_ARRAY; ++i)
{
ptr = fgets(buff, sizeof(buff), f);
if (ptr == NULL) perror("fgets") ;
j = sscanf(buff, "%s %s", &array1[i], &array2[i]);
if (j != 2) perror("sscanf" );
}
This is a guideline and it may or may not work correctly. The problem
that I see with your code is that you are defining the arrays as a
pointer. No storage has been allocated to hold the actual data. Your
usage of sscanf with the pointers is correct as far as I can tell, but
those pointers need to point to something. As it is, sscanf writes the
strings to random locations in memory. I'm amazed that you didn't get a
general protection fault or a segmentation fault when you ran this.

Also, arrays of strings are a little strange because a single string is
in itself an array of char, so to allocate an array of strings, you need
a 2 dimensional array of char.
--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
Jan 3 '06 #7

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

Similar topics

1
7056
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the file... Thank you to all of you who can help me with this one...
4
3162
by: nightflyer | last post by:
Hi all, [code snippet appended at the end.) my question: A class has a few string variables with not know length at design time. Now I declare lets say a 1000 of those classes and put them in a vector. Can I write that vector directly to a binary file?
8
18253
by: Phil Slater | last post by:
I'm trying to process a collection of text files, reading word by word. The program run hangs whenever it encounters a word with an accented letter (like rôle or passé) - ie something that's not a "char" with an ASCII code in 0..127 I've searched the ANSI C++ standard, the internet and various text books, but can't see how to workaround this one. I've tried wchar_t and wstring without success. But rather than spending lots of time on...
7
6063
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
4
3267
by: Gaijinco | last post by:
I had a file named nap.in which looks like this: 4 10:00 12:00 Lectures 12:00 13:00 Lunch, like always. 13:00 15:00 Boring lectures... 15:30 17:45 Reading 4 10:00 12:00 Lectures 12:00 13:00 Lunch, just lunch.
18
9075
by: John | last post by:
Hi, I'm a beginner is using C# and .net. I have big legacy files that stores various values (ints, bytes, strings) and want to read them into a C# programme so that I can store them in a database. The files are written by a late 1980's PC Pascal programme, for which I don't have the source code. I've managed to reverse engineer the file format. The strings are stored as Ascii in the file, with the first byte indicating the string...
2
22608
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be read (the file will be written by only this program); file can be either in text or binary (preferably binary as the files may be read repeatedly); the amount and size of strings in the array won't be known until run time (in the example I have it in...
9
4604
by: dgleeson3 | last post by:
Hello All I have a txt file of strings of different lengths. I dont know how many strings are in the file. I have no problem reading the file and sending to the console (as below). To store the strings read, in a buffer, I had decided to use an array of strings.
6
5736
by: Sabiyur | last post by:
Hi All, The application I am doing requires hundreds of strings to be stored & retrieve those randomly. I think I can use microsoft resource ".rc" file to store the strings. I am not sure, how to write strings in ".rc" file and retrieve it programmatically? Do you guys have any idea on this? Is there any good article on this?
6
3531
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features to an old program that I wrote in delphi and it's a good opportunity to start with c++.
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10081
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
9946
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
7494
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
5378
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...
1
4044
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
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.