LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-14-2004, 08:14 AM   #1
Menestrel
Member
 
Registered: Oct 2003
Location: Bucharest
Distribution: Debian Sarge, Slackware Current, Ubuntu
Posts: 183

Rep: Reputation: 30
reading from a file line by line


I'm quite new to programming so please bare with me.

I have a file which is structured like:

10.10.323.777 data1 non-important-data
10.11.111.888 data2 non-important-data

I want to read from this file in an array all the numerical values, and into another array all data. I don't want to read non-important-data
How can I do this ? Using C ? or using bash scrippting ?
 
Old 12-14-2004, 08:32 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,283

Rep: Reputation: 172Reputation: 172
arrays not great in bash, C long-winded for this sort of stuff.

use perl or tcl or awk

why two arrays?
 
Old 12-14-2004, 09:14 AM   #3
Menestrel
Member
 
Registered: Oct 2003
Location: Bucharest
Distribution: Debian Sarge, Slackware Current, Ubuntu
Posts: 183

Original Poster
Rep: Reputation: 30
why not two arrays ? they contain different data, I want to take these data from the file and make calculations based on them. Could you help me with the C code that would perform the reading from the file ? Can I use in the C file, commands like ping, tracert ?
 
Old 12-14-2004, 09:48 AM   #4
shmonkey
Member
 
Registered: Nov 2004
Location: UK
Distribution: Ubuntu
Posts: 118

Rep: Reputation: 15
This shows how to read in the data in as strings, you will need to convert the data to do your calculations. Not sure if all of the syntax is right but you get the jist.


Code:
#define MAX_ARRAY 100
char data_in[1024], data1[MAX_ARRAY][1024],data2[MAX_ARRAY][1024];
char *p;
int n=0,x;
FILE handle;

handle = fopen(your_file_name,"r");
if(handle == NULL)
	return 0; // error
	
while(!feof(handle) && (n < MAX_ARRAY))	
{
	if(fgets(data,1024,handle)!=NULL)
	{
		p = strtok(data," ");
		if(p)
		{	
			strcpy(data1[n],p);
			p=strtok(NULL," ");
			if(p)
				strcpy(data2[n],p);
		}	
		n++;
	}
	
	// print out data
	for(x=0;x<n;x++)
		printf("data1= %s   data2= %s\n",data1[x],data2[x]); 
}

Last edited by shmonkey; 12-14-2004 at 09:49 AM.
 
Old 12-14-2004, 09:53 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,283

Rep: Reputation: 172Reputation: 172
you haven't stated your problem clearly.


if the data are related having two arrays don't make sense.
use a mapping (associative, hash, whatever) array of of one to another.

you need to state the problem clearly.
what you got.
what you want.
what you expect.

regards, billy
 
Old 12-15-2004, 02:17 AM   #6
kamstrup
Member
 
Registered: Aug 2003
Location: Aarhus, Denmark
Distribution: Slackware, Ubuntu
Posts: 122

Rep: Reputation: 15
I would use python for such stuff at any time! Check out www.python.org for tutorials
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
File reading line by line and compare Goni Linux - Software 14 09-21-2005 12:24 AM
Reading a file and running a command for each line. mijohnst Linux - General 11 08-22-2005 06:18 PM
reading a line at a time in a file topcat Programming 2 12-14-2004 05:49 AM
reading a single line of a text file davi_cabral Linux - Software 1 10-29-2003 12:24 PM


All times are GMT -5. The time now is 03:39 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration