Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 09-22-2004, 12:07 AM   #1 (permalink)
toast28
Registered User
 
Join Date: Sep 2004
Posts: 21
toast28 is on a distinguished road
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;
}
toast28 is offline   Reply With Quote
Old 09-22-2004, 02:52 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 09-22-2004, 11:14 AM   #3 (permalink)
toast28
Registered User
 
Join Date: Sep 2004
Posts: 21
toast28 is on a distinguished road
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.
toast28 is offline   Reply With Quote
Old 09-22-2004, 02:30 PM   #4 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
strtol(3) will do everything you need. if the numbers are separated by white space, you don't even need to split the string.
joe_bruin is offline   Reply With Quote
Old 09-22-2004, 02:30 PM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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).
__________________
Valmont is offline   Reply With Quote
Old 09-22-2004, 07:38 PM   #6 (permalink)
toast28
Registered User
 
Join Date: Sep 2004
Posts: 21
toast28 is on a distinguished road
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.
toast28 is offline   Reply With Quote
Old 09-22-2004, 08:55 PM   #7 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
I'll probably be back with other problems before my Oct 7th deadline.
Sure. No problem, you're welcome.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamic allocation..urgent help needed!!! kashif Standard C, C++ 4 04-21-2003 09:50 AM


All times are GMT -8. The time now is 08:14 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting