Reading a spaced file

This is a discussion on Reading a spaced file within the C++ Programming forums, part of the General Programming Boards category; I have a text file with a paragraph of text in it, but in between each letter is a space. ...

  1. #1
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    Reading a spaced file

    I have a text file with a paragraph of text in it, but in between each letter is a space. I'm trying to get a program to read into the file and write a file with no spaces, but I'm not managing. I've tried something like this so far ( this isn't the actual code, I get frustrated and deleted it :P )

    Code:
    int main()
    {
    
    ifstream fin;
    ofstream fout;
    
    fin.open("myfile.txt", ios::binary | ios::in);
    if(!fin)
    {
    cout << "File not opened.";
    Sleep(2000);
    return 0;
    }
    
    char data;
    
    while(fin.get(data))
    {
    cout << data;
    }
    
    /* Up to here, my program works, and I get everything printed to screen. */
    Then I come across a problem. "data" wasn't declared as an array, so I can't use this :
    Code:
    for(int x = 0; x < strlen(data); x++)
    {
    cout << data[x]
    x++;
    }
    Also, I can't do this :
    Code:
    char content[strlen(data)] = data;
    for(int x = 0; x < strlen(data); x++)
    {
    cout << content[x]
    x++;
    }
    Does anyone see how I could solve this ?
    Thanks.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    1. If your are opening a text file,don't use ios::binary.
    2. get should work
    Copy with whitespace
    Code:
    char c;
    while(file.get(c))
         cout.put(c);
    Copy without whitespace
    Code:
    while(file.get(c))
        if(!std::isspace(c))
            cout.put(c);

  3. #3
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    I'm using ios::binary because the file was written in binary, it's actually a Windows file. If you try writing

    "Hello,

    Hi."

    in binary, you'll see you don't get a line between hello and hi, but a square character, which is what's in this file.

    What's cout.put and std::isspace ?

  4. #4
    Registered User happycoder's Avatar
    Join Date
    Jun 2003
    Posts
    6
    Also, I can't do this :

    Code:
    char content[strlen(data)] = data;
    for(int x = 0; x < strlen(data); x++)
    {
    cout << content[x]
    x++;
    }
    if im reading you correctly, you are trying to:

    1. create a new string the size of data
    2. copy the contents of data to the new string

    try replacing
    Code:
    char content[strlen(data)] = data;
    with this:
    Code:
    char *content = new char[ strlen(data) ];
    strcpy(content, data);
    dont forget to delete content.

    as for the loop, i'll leave it up to you to figure out what is wrong with it.

  5. #5
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    When and why would I delete content ?

    Also, I still don't see what's wrong with the loop.

    Code:
    for(int x = 0; x < strlen(data); x++)
    {
    cout << data[x]
    x++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21