How would I make cin.ignore all the spaces in the file?

Oct 19, 2018 at 12:39pm
What is the correct sytax for cin.ignore to make it ignore all the spaces in the file?
Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
     #include <fstream>
#include <iostream>

int main()
{

    char fileName[80];
    //char buffer[255];
    std::cout<<"Enter the filename to open.\n";
    std::cin>>fileName;
    
    //std::ofstream fout(fileName);
    //fout << "This line is written directly to the file.\n";
    //std::cout <<"Enter the text for the file:";
    //std::cin.ignore(1,'\n');
    //std::cin.getline(buffer,255);
    //fout << buffer <<"\n";
    //fout.close();
    
    std::ifstream fin(fileName);
    std::cout<<"Heres the contents of the file:\n";
    char ch;
    while (fin.get(ch))
        std::cout << ch;
        
        std::cout<<"\n***End of file contents***\n";
        
        fin.close();
}

Oct 19, 2018 at 12:55pm
for space : std::cin.ignore(256,' ');
for tab: std::cin.ignore(256,'\t');
for line : std::cin.ignore(256,'\n');

but these lines will ignore untill space, tab or newline

and if you want to just ignore the space it is better to use the getline
Last edited on Oct 19, 2018 at 1:06pm
Oct 19, 2018 at 1:27pm
By default, formatted input with >> ignores white space.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <fstream>

int main()
{
    std::ifstream file( __FILE__ ) ; // this file

    char c ;
    while( file >> c ) std::cout << c ; // read and print all non-space characters

    std::cout << '\n' ; // put a new line at the end (just being well mannered).
}

http://coliru.stacked-crooked.com/a/2c3d7b6e2f413574
Oct 19, 2018 at 1:28pm
How wouild I use getline to do that?

or if I use cin ignore where do I put it in the program?

everywhere I put it it stops the file output from printing but compiles fine without it.

#include <fstream>
#include <iostream>

int main()
{
char fileName[50];


std::cout<<"Enter the file name to open for reading.\n";



std::cin>>fileName;

std::ifstream fin(fileName);

std::cout<<"Here's the contents of the file:\n";
char ch;

while(fin.get(ch))
//std::cin.ignore(256,' ');
std::cout<<ch;

std::cout <<"***End of file contents.***\n";
fin.close();
}
Oct 19, 2018 at 1:40pm
p.s. JLBorges that doesn't work with char fileName[50];
Oct 19, 2018 at 2:02pm
in you code cin.ignore will ignore everything untill next space.
for examolae the result "Hello World" would be "HW"

but if you want you can yuse getline
like:
getline(cin, ch, ' ');
Oct 19, 2018 at 2:09pm
and beside useung char fileName[50]
use string fileName;
Oct 21, 2018 at 2:21pm
how can i make ifstream fin work with the string?

thanks

#include <fstream>
#include <iostream>
#include <string>

int main()
{
std::string fileName;
//char fileName;


std::cout<<"Enter the file name to open for reading.\n";



std::getline(std::cin,fileName);

std::ifstream fin(fileName);

std::cout<<"Here's the contents of the file:\n";
char ch;

while (getline(std::cin,ch,' '))
//while(fin.get(ch))
//while( fileName >> ch ) std::cout << ch ;
//std::cin.ignore(256,' ');
std::cout<<ch;

std::cout <<"***End of file contents.***\n";
fin.close();
}
Oct 21, 2018 at 3:11pm
how can i make ifstream fin work with the string?

Use a modern compiler that is compiling the program to one of the modern C++ standards, C++11, C++14, or C++17.

Depending on the compiler you're using you may need to inform it to use one of those modern standards by modifying the compile options. See the documentation for your compiler and IDE for more information.


Oct 23, 2018 at 9:19am
Im using c++14 now. How can I make the while loop compile?

thanks.



#include <fstream>
#include <iostream>
#include <string>

int main()
{
std::string fileName;
//char fileName;

std::cout<<"Enter the file name to open for reading.\n";

std::getline(std::cin,fileName);

std::ifstream fin(fileName);

std::cout<<"Here's the contents of the file:\n";
char ch;

while (std::getline(std::cin,ch,' '))
//while(fin.get(ch))
//while( fileName >> ch ) std::cout << ch ;
//std::cin.ignore(256,' ');
std::cout<<ch;

std::cout <<"***End of file contents.***\n";
fin.close();
}
Oct 23, 2018 at 9:31am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

int main()
{
   std::string fileName;
// std::cout << "Enter the file name to open for reading.\n";
// std::getline( std::cin, fileName );
// std::ifstream fin(fileName);
   std::ifstream fin( __FILE__ );      // this file

   std::cout<<"Here's the contents of the file:\n";
   std::string line;
   char ch;
   while ( std::getline( fin, line ) )
   {
      std::stringstream ss( line );
      while ( ss >> ch ) std::cout << ch;
      std::cout << '\n';
   }
   std::cout << "***End of file contents.***\n";
} 


Here's the contents of the file:
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>

intmain()
{
std::stringfileName;
//std::cout<<"Enterthefilenametoopenforreading.\n";
//std::getline(std::cin,fileName);
//std::ifstreamfin(fileName);
std::ifstreamfin(__FILE__);//thisfile

std::cout<<"Here'sthecontentsofthefile:\n";
std::stringline;
charch;
while(std::getline(fin,line))
{
std::stringstreamss(line);
while(ss>>ch)std::cout<<ch;
std::cout<<'\n';
}
std::cout<<"***Endoffilecontents.***\n";
}
***End of file contents.***


What do you want to do about linebreaks?
Last edited on Oct 23, 2018 at 9:47am
Topic archived. No new replies allowed.