473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

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

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
4 1183
Soneji
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
9,208 Expert Mod 8TB
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
Soneji
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
niskin
109 100+
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

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

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
4
by: Gregor | last post by:
I am trying to use an image map with a mouseover function to show various DIV tags. I am not an expert, but what I've come up with works in IE but not in FireFox. Any insight would be greatly...
4
by: jerryyang_la1 | last post by:
I've found this script that allows be to hide/show form elements.. <script language="JavaScript"><!-- var toggle = true; function show(object) { if (document.layers && document.layers)...
6
elamberdor
by: elamberdor | last post by:
Hi All! Trying to get a drop down hide/show div on a html page triggered by a button in flash. (Intro: Very very Little experience in dynamic flash) setup: html page, flash map on page, button...
2
by: jp2group | last post by:
I have a Main Form that hides in the Task Bar Tray as an Icon until one of the Tray Icon's Menu Items is selected. (think of your antivirus program running there) The program starts up fine and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.