By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,157 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,157 IT Pros & Developers. It's quick & easy.

don't hide your experience in this ... plz

P: 1
hi all ,
i want to extract word by word in a text file to use this in other application so i wrote that code but there is some problems in the way these words apperes(ie. one line doesn't appear till it's end) and so on

the code is

#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
void main ()
{
char word[15];
system ("D:\\Program Files\\Microsoft Visual Studio\\sakr.txt");
char str[100];
ifstream in("sakr.txt");
if(!in)
cout<<"not found\n";
while(in.getline(str,100))
{
int count=0;
for(int i=0;i<100;i++)
{
if (str[i]!=' ')
{
word[count]=str[i];
count++;
}
else
{

for(int k=count;k<15;k++)
word[k]=0;
count=0;
cout<<word<<endl;
}

}
}

in.close();
}



can u figure out wats wronge ..... or suggest any other solution

thanx in advance
Jun 21 '07 #1
Share this Question
Share on Google+
4 Replies


P: 36
Not sure if this will be any help, but I'll try...

What are you trying to accomplish with the 'system' call?
If it's to point to the text file, that won't work.

System is used to run a command in the console.
( For example: "cls" to clear the screen, or "exit" to exit the console. )

You can put the path to the file directly into the 'ifstream' constructor:
Expand|Select|Wrap|Line Numbers
  1. ifstream in("D:\\Program Files\\Microsoft Visual Studio\\sakr.txt");
  2.  

Let's try something... Try this code, and see if it does what you are trying to get your code to do:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. int main()
  8. {  
  9.   string input;
  10.   vector<string> words;
  11.  
  12.   ifstream in( "D:\\Program Files\\Microsoft Visual Studio\\sakr.txt" );
  13.  
  14.   if( !in.is_open() )
  15.   {
  16.     cout << "Not found...\n";
  17.     exit( 1 );
  18.   }
  19.  
  20.   while( !in.eof() )
  21.   {
  22.     in >> input;
  23.     words.push_back( input );
  24.   }
  25.  
  26.   in.close();
  27.   in.clear(); 
  28.  
  29.   for( int i = 0; i < words.size(); i++ )
  30.   {
  31.     cout << words[i] << '\n';
  32.   }
  33. // If you want all the words on one line with spaces, change the  line above to: cout << words[i] << ' ';
  34. // If you want all the words on one line with NO spaces, change the line above to: cout << words[i];
  35.  
  36.   return 0;
  37. }
  38.  
( BTW: I added the 'clear' on line 28 because the 'while' loop above it will most likely set the 'fail', and 'bad' bits.
The 'clear' will reset to 'goodbit' so you can open the file again without hassle. )

If this is what you are trying to get out of your code, then I may be able to fix it.

I'm working on it now. I'm sure some of the experts here can fix it, if I can't.
Lord knows, they've helped me a few times! :)

I'm off to work on your original code. Let me know if this code does what you want.
Jun 21 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
You are making this too hard.

Use the >> operator. It skips whitespace.

Given this file:

Mary had a little lamb
It's fleece was white as snow

you extract the words with this code:

Expand|Select|Wrap|Line Numbers
  1. ifstream input("C:\\scratch\\instructor\\test.txt");
  2. string str;
  3. while (!input.eof())
  4. {
  5.   input >> str;
  6.   cout << str << endl;
  7. }
  8.  
Part of the trouble here is writing in C when you should be writing in C++.
Jun 21 '07 #3

P: 36
I am making it too hard, or Mohammed is?

:)

I only added the vector because he wanted to take the words from a file, and use them in another application.
I figured the vector was an easy way to store those words.

I was going to use a few strings, but I think vectors are so cute. LOL.

Hmm... This leads into a question. Be back later, I need to post this question...

Lates!

-Soneji
Jun 21 '07 #4

100+
P: 109
Expand|Select|Wrap|Line Numbers
  1. char read[1024];
  2. ifstream chat ( "C:\\file.txt" );
  3. chat>> read;
  4. chat.close();
  5.  
Do what you want with read once you have put the text in there.

Or if you want to do it word by word then:
Expand|Select|Wrap|Line Numbers
  1. chat>> reada >> readb >> readc >> readd >> reade >> readf >> readg >> readh >> readi >> readj >> readk >> readl >> readm >> readn >> reado >> readp >> readq >> readr >> reads >> readt;
  2. chat.close();
  3. cout<< reada <<" " << readb <<" " <<  readc <<" " << readd <<" " << reade <<" " << readf <<" " << readg <<" " << readh <<" " << readi <<" " << readj <<" " << readk <<" " << readl <<" " << readm <<" " << readn <<" " << reado <<" " << readp <<" " << readq <<" " << readr <<" " << reads <<" " << readt;
  4.  
Jun 23 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.