Reading text file in C++ and output formatting - c++

I read a text file in C++ using this code:
void main()
{
clrscr();
ifstream o("file.txt",ios::in);
while(!o.eof())
{
o.get(w);
cout<<w;
}
getch();
}
but output shows empty spaces between individual letters in the file,
although there are no such spaces in the original file.
Example: If file has hello, my output shows h e l l o.
How to rectify?

Try this
while (o>>w) cout<<w;
And since you re dealing with unicode, char wont work out. Try using
wchar_t.

Related

C Builder (C++) AnsiString Length method

I am used to program in c#, but now i had to help my roommate with a c++ project.
This is the "not working code" :
void HighlightKeyWords::Highlight(TRichEdit eMemo,TRichEdit RichEdit1)
{
ifstream file("KeyWords.txt");
AnsiString temp;
int maxWordLength=0;
if(file.is_open())
{
while(file>>temp)
{ if(temp.Length()> maxWordLength)
{
maxWordLength=temp.Trim().Length();
}
keyWords.push_back(temp);
}
file.close();
}
else
{
ShowMessage("Unable to open file. ");
}
for(unsigned i=0;i<KeyWords.size();i++)
{
richEdit1->Text=KeyWords[i];
}
eMemo->Text=MaxWordLength;
}
I get a list of keywords from the file. In MaxWordLength i want to know to maximum length of a word ( words are separated by new line in the text file ). When I do the temp.Length, i get 695 ( the number of all characters in the file ). Why am I not getting the actual length of the word i am adding to the vector?
Thank you!
LE: I also did the MaxWordLength logic in the for below, the for where i put the items in the RichEdit.
Use file.getline() instead of the >> operator, which won't produce the desired output in your case, but gives you the full file content as result. So AnsiString().Length() is not your problem. Just modify part of your code to get it working as intended:
char buffer[255];
if(file.is_open()){
while(file.getline(buffer, sizeof(buffer))){
temp = AnsiString(buffer).Trim();
if(temp.Length()> maxWordLength) maxWordLength=temp.Length();
keyWords.push_back(temp);
}
file.close();
}

C++ Semicolon separated file reading

I basically have a semicolon separated text file, in that file there are some commands like "A", "P", "R", "S", and the inputs to process according to those commands like names "Ali Aksu, Mithat Köse", like transactions "Process, withdraw". I have a program which process those inputs without any problems in console (User gives the inputs). But i need to make it getting the inputs from the semicolon separated file. Here is a test for the reading:
This is an example input file:
A;Ali;Aksu;N;2;deposit;withdraw
P
A;Mithat;Köse;P;3;deposit;credit;withdraw
This is the output on the console:
A/Ali/Aksu/N/2/deposit/withdraw
P
A/Mithat/Köse/P/3/deposit/credit/withdraw
/
1.Problem: It cannot read the special characters like "ö"
2.Problem: Why is that starting with this weird "" character?
#include <iostream>
#include <fstream>
using namespace std;
int main(){
setlocale(LC_ALL, "Turkish");
fstream myfile;
char *string;
string = new char[50];
myfile.open("input_file.txt",ios::in);
while(!myfile.eof()){
myfile.getline(string, 49, ';');
cout << string << "/";
}
myfile.close();
cout << endl;
system("pause");
return 0;
}
I will assume that the file is in UTF8 format. If so then you question is really, how to i read UTF8 files using c++
here is somebody reading chinese How to read an UTF-8 encoded file containing Chinese characters and output them correctly on console?. You should be able to adapt this to your locale

File read and write while reading the file line by line

Program:
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
FILE *f;
char* line;
size_t ln=100;
char* s;
line=new char[100];
s=new char[100];
cout<<"input key"<<endl;
cin>>s;
f=fopen("parvin.txt","r");
if(f==NULL)
{
cout<<" no file TO read so creating for writing "<<endl;
//return 0;
f=fopen("parvin.txt","w");
fputs(s,f);
fputc('\n',f);
}
else
{
while(! feof(f))
{
fgets(line,100,f);
cout<<line<<endl;
//if(!strncmp(line,s,strlen(line)-1))
if(strcmp(line,s)== 0 )
{
cout<<"duplicate found"<<endl;
fclose(f);
return 0;
}
}
fclose(f);
f=fopen("parvin.txt","a+");
fputs(s,f);
fputc('\n',f);
}
fclose(f);
}
Here the above program where I like to read an input string and write it into file provided the string is not present already in file.
take input string
open file in read mode.
if it is first time entry file will not be there if file pointer return NULL, create a file to write mode and write the
inputted string.
if file already there then read file line by line and compare with input string if match with any line then return and close.
other wise open the same file in write mode and write the inputted string.
But it is not working properly..
strcmp not executing properly.... with the duplicate entry also it
dont go into that loop of "duplicae found" .
please if anyone can help ...
The fgets:
fgets(line,100,f);
consumes the newline character from f and stores it in line. But s doesn't contain the newline character. So, the strcmp returns a non-zero number as the strings(s and f) are different.
Strip the newline character by using
line[strcspn(line, "\n")] = '\0';
just after the fgets. The strcspn function, in your case, returns the number of characters until a \n in line. If \n is not found in line, it returns the length of the string line(strlen(line)).
Also, read Why is while ( !feof (file) ) always wrong?. Replace
while(!feof(f))
with
while(fgets(line,100,f)) //Same as `while(fgets(line,100,f) != NULL)`
and don't forget to remove the fgets from the body of the loop to fix this issue.
Use
while(fgets(line,100,f)!=NULL)

How Read Urdu text file and then write it to other file in c++

I want to input a file containing Urdu words and write them in other file. The problem I'm facing is that Urdu language doesn't have spaces, so other file written would look alike all words joined to each other. How can i separate words or detect spaces ? My Code is this.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
ifstream file;
ofstream myfile;
file.open ("urduwords.txt"); //File containing Urdu words
myfile.open("urduoutput.txt"); // Output file
if (!file.is_open()) return;
char * word= new char[];
while(file>>word)
{
myfile<<word;
// What to write here to separate words or detect spaces. Input file is saved in UTF-8
}
myfile.close();
file.close();
cout<<"Check output"<<endl;
}
Oh I got answer. The answer is you have to put spaces between Urdu characters because Urdu language has Space Omission Problem so at while loop
while(file>>word)
{
myfile<<word;
myfile<<" "; // Put spaces between words.
}

C++ - Read and print whole file

Like in the topic, I'd like to read from standard input and print to standard output whole file with no difference between them.
program < data.txt > data.out
diff data.txt data.out // <- the same
File contains Unicode letters.
I've managed write following piece of code:
char s[100000];
int main()
{
setmode(1, _O_BINARY);
char *n;
do {
n = gets(s);
s[strlen(s)-1] = '\n';
printf("%s", s);
}
while(n);
return 0;
}
but input and output is slighty different (input: 76 465KB, output: 76 498KB)
Thanks in advance.
EDIT:
Now, it's only 2KB difference.
EDIT:
It's OK.
It could happen if the input file has \n line endings. The output file will have \r\n line endings on Windows. That could explain the difference.
If you don't want to output the \rs, you can follow this answer