Sign in to follow this  
JinJo

simple file input question

Recommended Posts

Hi I havn't done I/O using streams for ages and now I have to do it for Uni. if i had a file with a list of actions in it in the form "current state, current symbol, new state, new symbol, move direction" e.g. q0, 1, q2, ^, R . . . how would I go about gettin the values into variables and skipping the commas and spaces?

Share this post


Link to post
Share on other sites
if you're using C/C++ you could always use the strtok function. First read in the entire line and then pass it to the function.

I think java has a similar string tokeniser function.

Share this post


Link to post
Share on other sites
Quote:
Original post by crazy_andy
personally I would read in the entire statement into an array, and check each value for being a comma, space etc.
store the numbers of the ones you want and then copy those values into the relevant variables


this is unnecessary & redundant and if you take this route your more than likely be re-writing code that already exists & is standardized.

Quote:
Original post by Gav
if you're using C/C++ you could always use the strtok function. First read in the entire line and then pass it to the function.

I think java has a similar string tokeniser function.


If your using C++ (for that matter even C) stay away from it at all costs the main reason being:

Quote:

Never use this function. This function modifies its first argument. The identity of the delimiting character is lost. This function cannot be used on constant strings.


besides the point he does not wont to just simply tokenize a string in memory into string tokens/words.


@JinJo: If your using C++ then i suggest you look into stream & stream buffer iterators with STL containers & algorithms find out more about the standard template library here and stream iterators here, here is a taster example:


#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <fstream>
#include <iostream>

int main(int argc, char* argv[]) {

if(argc == 1) {
std::cerr << argv[0] << " error usage: " << argv[0] << " <file-name>\n";
return EXIT_FAILURE;
}

std::ifstream ifs(argv[1]);

if(!ifs) {
std::cerr << argv[0] << " error could not open/read file\n";
return EXIT_FAILURE;
}


std::remove_copy(std::istreambuf_iterator<char>(ifs),
std::istreambuf_iterator<char>(),
std::ostreambuf_iterator<char>(std::cout),
',');


return EXIT_SUCCESS;

}


its not the best example, i could show you more useful code if you could elaborate more on the problem your trying to solve and more about that file format, if its something more complex than i'm assuming then there might be a better alternatives such as using tools etc probably not its hard to say until we know what your intentions are.

Share this post


Link to post
Share on other sites
If you in fact have control over the file format, I would strongly recommend not putting in the commas at all; then you can just read each item with "cin >> var". Of course you'll need to pick an appropriate type for each variable.

Let me guess... Turing machine emulator?

Share this post


Link to post
Share on other sites
Cheers for all the reply's.
I went and hard coded the file into my source (I know that's bad but I only had half an hour to finish the whole thing).

Zahlman: Your right it is a TM simulator and I have it all working for the specific TM I was to design.

I will take your advice and look more into STL (don't know why I havn't done much with it already as I have been coding in c++ for about 3 years now, just forgot some basic things here)

Also I didn't have control over the file format as far as I knew of otherwise I would have just done cin >> val etc.

Cheers anyway.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this