Anyone able to array txt file into cocrrect form?  
Author Message
casfus





PostPosted: Visual C++ Language, Anyone able to array txt file into cocrrect form? Top

i am a newbie on C++,since i have searched so many books and question ppl asked here, but i can't solve this problem yet, i need some help.does anyone able to help me out

i want to array the txt name 1.txt and save it in 2.txt with new array form.

vocabulary1

other form of vocabulary

definition1......................single line

definition2.....................single line

definition3.....................single line

definition#.....................single line

</>

vocabulary2

form of vocabulary2

............................and so on, many words continue like that

with </>separate from 2 vocabularies

the request is array the txt1 into like this:

vocabulary1|other form of vocabulary1

definition1......................<BR>definition1......................<BR>(all in one line)

</>(one line with it separate two different vocabularies)

vocabulary2|form of vocabulary2(one line)

All definitions combine into one line,and put<BR>to connect each other, the line with </> not changed.

i know how to open a file and use getline(). with getline(input,str,'\n')

i only able to copy first line of it, or use getline(input,str,'\0') copy all words inside it.

that's not what i want to get, i just want to get a function to solve the problem with array them in a right way. (doesn't need to write the head of program. sorry for my broken language.

Thanks in advange. have a good day everyone.




Visual C++13  
 
 
Marius Bancila





PostPosted: Visual C++ Language, Anyone able to array txt file into cocrrect form? Top

Did you at least try to write something Can you share it with us

 
 
Sarath.





PostPosted: Visual C++ Language, Anyone able to array txt file into cocrrect form? Top

You question is not complete. As a newbie, it's better to use some wellknown and implemented file formats.

Why you are not trying an xml file
See the samples below!

http://www.example-code.com/vcpp/xml.asp
http://www.informit.com/articles/article.asp p=100670&rl=1

You can also try with INI files.
http://msdn2.microsoft.com/en-us/library/ms725501.aspx



 
 
casfus





PostPosted: Visual C++ Language, Anyone able to array txt file into cocrrect form? Top

#include<iostream>
#include<string>

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

int main(){
  string s;
  ifstream in("1.txt");
  char ch;
  ofstream out("2.txt");
  while(getline(in,s))
   { 
       cout<<s<<endl;
       out<<s; /**the result put in 2.txt make  all lines into one line, still don't know how to make third line become second line,forth and fifth.... into secondline until meet line with </>, and the system run again after linewith </>,i might get the result later, i have learnt C++ for around one week, since i got a program able to convert other dict form into the form like the one i show, but i want to change the form into what i need.and suit for my other dictionary txt form. **/
       cin.get();

    }

}

i am not good enough to learn xml and ini type. i just want to make it simple and  get the the project to succeed.

if anyone get it, pls let me know. thanks alot telling me the info, even though i can't learn it yet.



 
 
Sarath.





PostPosted: Visual C++ Language, Anyone able to array txt file into cocrrect form? Top

Oh....You forgot to put endl at the end out<<s; that's what created the issue

Please modify your code with

out<<s<<endl;

if the above one is not solving your issue, please try

out<<s<<"\r\n"; //Carriage return and New line


 
 
casfus





PostPosted: Visual C++ Language, Anyone able to array txt file into cocrrect form? Top

yes ,it works now. thanks a lot, Sarath. now i will need to find a way to determine which line i should output with endl; which function do u need to tell the program

should i get the line number function to tell the program to do what i want   to get all line number first ,then let the program to judge i need to find good way to solve it.



 
 
Sarath.





PostPosted: Visual C++ Language, Anyone able to array txt file into cocrrect form? Top

Actually I was thinking why you are reading each line of a file and writing it to another one instead of using read/write functions of stream classes!

I could not fully understand your requirement.

It seems you are supposed to read a line and based on some criteria, you need put a line break at the destination file.

e.g:
Input file
-----------
First Line
Second Line
Third Line // here you need a line break
Fourth Line
Fifth Line
....

O/P File
First LineSecond LineThird Line
Fourth LineFifth Line
.....

Is this the desired output If yes what is the criteria for a new line in the destination file



 
 
casfus





PostPosted: Visual C++ Language, Anyone able to array txt file into cocrrect form? Top

i would like to stack first line with second line,put |between them.

third line forth line fith line stack together.and use <BR>to joint them,depend on how many definitions a word with until get a line with </> 

when read first line  out<<s;    ( this is output of file)

read second line  out<< "|" << s <<endl;

read third line downward  out<< s << "<BR>"; until reach a line with </>     when getl a ine with </>    out<<"\t\n"<<s;

then run again after line with </> and state second word(vocabulary2) as first line and do it again, and so on. this is dictionary txt file type

word1|word1 other form(first line)

definition1<BR>definition2<BR>......(put them into one line)

</> (i will use ultraedit to remove it after i get my correct output, there should be blank line between two words)

word2|word2 other form (sample: CPU|central processing unit

1. central processing unit.<BR>2. Occasionally used (although less and less) to refer to the system unit.   (all meaning into one line, and </>with a line to seperate words and meaning)

there's around over 200 thousands of words, so i need to write a program to array them

hehe, i don't know if i make is specify yet.  thanks again reply me.

here's my new source(suggest one word only(1), one word other form(2),with 4 lines definitions, i array them like that)

#include<iostream>
#include<string>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
  string s;
  ifstream in("1.txt");
  ofstream out("2.txt");
  int i=0;
  while(getline(in,s))
   {
      if(i==0||i==2)
      out<<s;
     
      else if(i==1)
      out<<"|"<<s<<endl;
      else
      out<<"<BR>"<<s;
       i++;
    }
 }

i don't know how to let the program to repeat doing other words, there are so many words and lines in the txt , i still don't know to tell the program to array them.