LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-14-2008, 03:15 PM   #1
emp1953
LQ Newbie
 
Registered: Nov 2007
Posts: 18

Rep: Reputation: 0
use fscanf to read text from a file


Working in Linux with g++ compiler

Trying to implement this functionality

I have a text file

SOURCE_IP_ADDR 129.145.225.100
TARGET_IP_ADDR 129.145.244 200
SOURCE_UDP_PORT 5500
TARGET_UDP_PORT 5600
SOURCE_TCP_PORT 5200
TARGET_TCP_PORT 5400

want to read the text file as follows

code follows

#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <stdio.h>


std:: string paramArray[10][1]; declared

I open the file with
FILE *inFile = fopen("in.txt", "r");


I would really like to read the file while not eof
as
while (not eof){
fscanf(inFile, "%s %s", paramArray(i,0), paramArray(i,1));
i++;
}
fclose(infile);

I get segmentation faults, and data in the dataarray is not understandable.

What am I doing wrong?
or
What do I need to do to make it work correctly.

Thanks
emp1953
 
Old 03-14-2008, 03:41 PM   #2
emp1953
LQ Newbie
 
Registered: Nov 2007
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by emp1953 View Post
Working in Linux with g++ compiler

Trying to implement this functionality

I have a text file

SOURCE_IP_ADDR 129.145.225.100
TARGET_IP_ADDR 129.145.244 200
SOURCE_UDP_PORT 5500
TARGET_UDP_PORT 5600
SOURCE_TCP_PORT 5200
TARGET_TCP_PORT 5400

want to read the text file as follows

code follows

#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <stdio.h>


std:: string paramArray[10][1]; declared

I open the file with
FILE *inFile = fopen("in.txt", "r");


I would really like to read the file while not eof
as
while (not eof){
fscanf(inFile, "%s %s", paramArray(i,0), paramArray(i,1));
i++;
}
fclose(infile);

I get segmentation faults, and data in the dataarray is not understandable.

What am I doing wrong?
or
What do I need to do to make it work correctly.

Thanks
emp1953

NEVERMIND on this one, I figured it out.
But this leads to another question. I have the data in paramArray.
I need that data visible to all methods in my application.
How can I give this array global scope?

Thanks
 
Old 03-14-2008, 04:08 PM   #3
emp1953
LQ Newbie
 
Registered: Nov 2007
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by emp1953 View Post
NEVERMIND on this one, I figured it out.
But this leads to another question. I have the data in paramArray.
I need that data visible to all methods in my application.
How can I give this array global scope?

Thanks



Ok, I've been soundly thrashed about using a global variable.

What then are my options?
 
  


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
how to read text file using bash script kkpal Linux - Newbie 4 03-12-2008 02:57 AM
Need C++ Program to Read from Text File waXed Programming 5 10-26-2007 02:21 PM
how to read optional characters %*[] with fscanf? phyx Programming 1 01-18-2007 09:50 PM
need something that can read a Microsoft Works text file joshknape Linux - Software 1 10-30-2005 12:45 AM
Script to read a simple text file ravykanth Linux - Newbie 9 10-22-2003 05:15 AM


All times are GMT -5. The time now is 02:28 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration