LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 10-02-2011, 07:32 PM   #1
lk181
LQ Newbie
 
Registered: Oct 2011
Posts: 4

Rep: Reputation: Disabled
C++ Read in from file into strings and arrays


Okay so I took Intro to C++ and I was really good at it but that was a year ago. Right now I need to recreate a file header.

Title Card Required mo=5
Alpha MO coefficients
-3 -4.970736 -4.970736 -4.736883
31 0.333333 0.000000 0.000000
31 0.000000 0.333333 0.000000
31 0.000000 0.000000 0.333333
8 8.000000 0.000000 0.000000 0.233853
1 1.000000 0.000000 1.492645 -0.935410
1 1.000000 0.000000 -1.492645 -0.935410
1 5

I need to set to take the absolute value of the -3 and set it equal to some int. Then take the three 31 values and set those equal to some other int. My problem is I'm reading it in from a file and I can't get the file to stop reading so that I can set the numbers equal to what I need.

Help please.
 
Old 10-02-2011, 09:02 PM   #2
flamelord
Member
 
Registered: Jun 2011
Distribution: Arch Linux
Posts: 151

Rep: Reputation: 34
I'm not sure I quite understand what your problem is. What do you mean by "I can't get the file to stop reading?"

Are you asking you you can read just the one number?

If that is the case there are a couple things you can do. If you are using a file stream (the object oriented way to do it), then you can read the number like so:

Code:
int val1;
fstream >> val1;
Secondly, you can use the fscanf function, like so:

Code:
#include <stdio.h>

int i;
...

fscanf( fid, "%d", &i);
thsis is how you would do it in c, but it works in c++ as well.
 
1 members found this post helpful.
Old 10-25-2011, 05:03 PM   #3
lk181
LQ Newbie
 
Registered: Oct 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
thank you. I see that wasn't worded properly now but I fixed it.
 
  


Reply



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
Help Me(how to read text file using arrays in c++) rezen85 Programming 2 10-19-2010 09:50 AM
[SOLVED] Reading in a file in C, after file read null terminator at strings end gone josip76 Programming 2 05-14-2010 12:44 AM
java splitting strings into arrays trscookie Programming 3 07-06-2006 03:49 AM
Problem in C: arrays, functions and strings OrganicX Programming 15 03-18-2003 09:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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
Open Source Consulting | Domain Registration