|
 |
|
 |
09-22-2004, 12:07 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Sep 2004
Posts: 21
|
Segment string and change to int
OK, so I'm working on an assignment but I'm stuck on a pretty simple task. I am inputting a string of numbers from a file, but when I get the string in I'm having trouble cutting it up into its 4 sections. Once I figure that out I should be able to convert to int easily with atoi()
I don't have much code, but I'll post it anyways
Code:
// generate.cpp = a program to generate a file full of random sequence of print j
#include"stdinc.h"
#include"heap.h"
#include"print_job.h"
using namespace std;
int main(int argc, char* argv[])
{
ifstream fp(argv[1]);
if (fp)// if filestream works
{
for(int i=0;i<10;i++)
{
print_job job;
char* current;
current = new char[60];
fp.getline(current,60);
// need to cut current up into its 4 parts, search for spaces??
atoi(current);
}
}
else
cout << "Error Opening File";
return 0;
}
|
|
|
09-22-2004, 02:52 AM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
|
look at strtok() or perhaps some fscanf() for pattern matching.
Or maybe just a simple:
Code:
cin >> number1 >> number2 >> number 3 >> number4;
The last line is just something I remember to have seen, not nessesary something that will work.
|
|
|
09-22-2004, 11:14 AM
|
#3 (permalink)
|
|
Registered User
Join Date: Sep 2004
Posts: 21
|
Thanks for the advice Redhead. fscanf() looks like it may be what I need, but I'm not understanding the implementaion of it. Well I gotta go to work, but I'll be working on this all night so any help would be appreciated. I don't know if it will help, but the data from the file will be stored in a class, 1 line per item.
|
|
|
09-22-2004, 02:30 PM
|
#4 (permalink)
|
|
LOAD "*",8,1
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
|
strtol(3) will do everything you need. if the numbers are separated by white space, you don't even need to split the string.
|
|
|
09-22-2004, 02:30 PM
|
#5 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Re: Segment string and change to int
Code:
// generate.cpp = a program to generate a file full of random sequence of print j
#include"stdinc.h"
//USE THIS: #include <algorithm> INSTEAD OF "heap.h"
//IF you mean heap.h that comes with C++. If it is your (a custom heap library) then nevermind.
#include"heap.h"
#include"print_job.h"
#include <cstdlib> //for atoi()
#include <iostream>
#include <string>
#include <fstring>
using namespace std;
class MyThreeDataItems
{
public:
string First;
string Second;
string Third;
}
int main(int argc, char* argv[])
{
MyThreeDataItem MTDI;
ifstream fp(argv[1]);
if (fp)// if filestream works<=What's that? If file is opened correctly.
{
for(int i=0;i<10;i++)
{
print_job job;
// "using namespace std"? Then try to use standard C++ indeed. This -> char* is old and seldom needed.
fp >> MTDI.First >> MTDI.Second >> MTDI.Third;
int i; //int, double, whatever. I can't know.
i=atoi(First.c_str() );
//etc.
}
}
else
cout << "Error Opening File";
return 0;
}
It all depends on the format of the file you are loading from. The above works for a tab delimited file (amongst).
__________________
|
|
|
09-22-2004, 07:38 PM
|
#6 (permalink)
|
|
Registered User
Join Date: Sep 2004
Posts: 21
|
Thank you Valmont, using your code as a guide I got it to work. But don't worry, as a noob around here, I'll probably be back with other problems before my Oct 7th deadline.
Also want to say how much I appreciate a forum like this for programming students, my teacher is expecting us to remember things from last class, but I've taken 2 yrs off, so last class was forever ago. I don't know if I would be very useful, but if I see a problem I know how to solve, I'll try to respond. I really should know lots of this as a JR in a CS program, but we'll see, lol.
|
|
|
09-22-2004, 08:55 PM
|
#7 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Quote:
|
I'll probably be back with other problems before my Oct 7th deadline.
|
Sure. No problem, you're welcome.
__________________
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 10:29 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|