0

I have to write a function that will read input from a file. The file is set up: one character, space, word, space, throughout the file, like such:

A space 1 space 2 space... etc

I need to extract the whitespace following the one character and NOT the whitespace following the word.

How can I go about doing this? Should I just make it so the function writes the whitespace itself instead of extracting it?

Also, I am importing this info into a 2-d char array. Will I run into problems trying to write integers to a char array?

Rapptz
  • 20,807
  • 5
  • 72
  • 86
Bobazonski
  • 1,515
  • 5
  • 26
  • 43
  • 1
    `std::cin::unsetf(std::ios::skipws)` should be a start – sehe Jan 30 '13 at 00:45
  • It's for class and we're limited to certain tools. We haven't been taught tokenization yet so I'd get in trouble for using it. – Bobazonski Jan 30 '13 at 00:47
  • what are you saying? You can't use the standard library? It's in the C++ standard... (Otherwise, std::getline is your only viable way out) – sehe Jan 30 '13 at 00:50
  • Why would you get in trouble for using stuff you haven't been taught? What is this, sunday school? (now, being required to explain the code is entirely different matter -- you shouldn't use code you don't understand) – Eugene Jan 30 '13 at 01:03
  • @Eugene: when students use code they haven't been taught, that's usually because the copied it from stack-overflow or something, without learning the material. That's cheating. It's also unnecessary. – Mooing Duck Jan 30 '13 at 01:07
  • @Mooing Duck fair enough for library functions I guess (which should be explicitly forbidden if that is the purpose), but wrong for concepts. There is nothing wrong with using state machine to do some hairy logic exercise that was intended to be done with bunch of conditionals for example. – Eugene Jan 30 '13 at 01:13
  • 2
    I would be happy if students learned from good SO posts as long as the actually *learn* it. They're simply making you do it a specific way to verify that you learned the recently taught material. – doug65536 Jan 30 '13 at 01:14
  • @Eugene: Because, often, the teacher doesn't understand the more "advanced" code. And, yes, I know "advanced" is not really the proper term. – Lightness Races in Orbit Jan 30 '13 at 01:20
  • @MooingDuck that would _never_ read whitespace, unless you set noskipws – sehe Jan 30 '13 at 01:20
  • @user1362548: Reading some of the whitespace (especially if there's an unknown number of whitespace characters between) is actually very advanced. I think you misunderstood what's required of you here. – Mooing Duck Jan 30 '13 at 02:04

3 Answers3

2

Something like this maybe?

#include <iostream>
#include <fstream>

int main() {
    char myChar;
    char theWS;
    std::string word;
    std::ifstream in("example.txt");

    while(in >> myChar >> std::noskipws >> theWS >> word >> std::skipws) {
        std::cout << myChar << theWS << word << '\n';
    }
}
Rapptz
  • 20,807
  • 5
  • 72
  • 86
  • This assumes there's only one whitespace char, not sure if that's a valid assumption or not – Mooing Duck Jan 30 '13 at 02:03
  • @MooingDuck I made the assumption based on what the OP said -- "The file is set up: one character, space, word, space, throughout the file" – Rapptz Jan 30 '13 at 02:04
1

You should've been exposed to the idea of a tokenizer by now. This is the structure you need.

tadman
  • 208,517
  • 23
  • 234
  • 262
-1

You will be fine writing integers into character arrays. Since C and C++ represent ascii characters as small numbers anyways, handling them is easy. Some examples of the number values which correspond to specific chars: '0' => 48, '1' => 49, ... , 'A' => 65, 'B' => 66, etc.

Take a look at http://www.asciitable.com/ for the full set of ascii characters and their corresponding values.

This also allows you to perform mathematical operations on characters such as 'A' + 1 => 'B' as well as convert between numbers and characters (char) 65 => 'A'