how do i skip a char in filestream?
i tried the ignore function but that doesnt work in filestream, i also tried using an array[i] and said if(arr[i]=="-")
then i++ while outputting but that doesnt work either

Recommended Answers

Let's see the code you tried.

Jump to Post

To me this code is working too hard. Here's my take on it.

/******************************************************************************

                              Online C++ Compiler.
                https://www.onlinegdb.com/online_c++_compiler
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <fstream>
using …
Jump to Post

All 6 Replies

Let's see the code you tried.

commented: #include <iostream> #include <fstream> #include <string> using namespace std; void read (ifstream & in, ofstream & out) { int a[20]; string temp; +0
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void read (ifstream & in, ofstream & out)
{

    int a[20];
    string temp;
    int i=0;
    int j=0;

    in.open("phnum.txt");

    while(!in.eof() )
    {
              in>>a[i];
              i++;
    }

    in.close ();

    j=i;

    for(int i=0;i<j;i++)
    {
        cout<<a[i];
    }
    cout<<endl;

    int b[20];
    for ( i=0;i<j;i++)
    {
        if( a[i] != '-')
        {
            b[i]=a[i];
        }
        else
        {
            cin.ignore(1);
        }
    }

    for(int i=0;i<j;i++)
    {
        cout<<b[i];
    }

    out.open("outFile.txt");

    for(int i=0;i<j;i++)
    {
        if(a[i] != '-')
        {
            out<<a[i];
        }
        else
        {
            i++;
        }
    }

}
int main ()
{
    ifstream in;
    ofstream out;

    read (in, out);


    system("pause");
    return 0;

}

I'v tried it with char array and string as well using ignore function but nothing seems to work and it doesnt output anything either

commented: Did you notice the code doesn't out.close()? +15

To me this code is working too hard. Here's my take on it.

/******************************************************************************

                              Online C++ Compiler.
                https://www.onlinegdb.com/online_c++_compiler
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <fstream>
using namespace std;

void read ()
{
    ifstream in;
    ofstream out;
    int a;

    in.open("phnum.txt");
    out.open("outFile.txt");

    while(!in.eof() )
    {
        in>>a;
            if(a != '-')
            {
                out<<a;
            }
    }
    in.close ();
    out.close ();
}

int main ()
{
    read ();
    system("pause");
    return 0;
}

I see a possible gaffe on my part. Line 16 calls out an int and we are comparing a char on line 26. Possible fix? Change int to char on line 16.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.