Can't read from file - c++

I'm trying to read text from a .log file. I originally have it working a a windows console application but I'm now trying to make a GUI version using MFC. The problem is when i set the while loop as while(file.good()), it completely skips the loop and I'm not sure why because there are no errors with file.open()
void Conversion::toXml(CString openPath, CString savePath)
{
CString null("/0");
int c = openPath.GetLength(),q=0;
while(q<c)
{
if(openPath[q] == '\\')
{
openPath.Insert(q,'\\');
q++;
}
q++;
}
CStringA charstr(openPath);
const char* oPath((const char *) charstr);
CStringA charstr2(savePath);
const char* sPath((const char *) charstr2);
ifstream openFile;
ofstream savedFile(sPath);
string recText = "";
string temp;
openFile.open(oPath);
while(openFile.good())
{
getline(openFile,temp);
recText = recText + temp;
}
smaller version(should compile)
#include <iostream>
#include <fstream>
#include <string>
#include "stdio.h"
#include <windows.h>
#include <algorithm>
#include <atlstr.h>
using namespace std;
int main()
{
string recText = "";
string temp;
CString path = "C:\\Users\\name\\Desktop\\2012-08-281.log";
CStringA charstr(path);
const char* oPath((const char *) charstr);
ifstream xmlDoc (oPath);
ofstream finished ("C:\\Users\\name\\Documents\\example3.xml");
while(xmlDoc.good())
{
getline(xmlDoc,temp);
recText = recText + temp;
}
finished<<recText<<endl;
return 0;
}

The problem might be that you try to escape the backslash when you don't need it, adding unnecessary backslashes in the path. Escaping backslashes is only needed for string literals. It might be that Windows doesn't like paths with multiple consecutive backslashes.

Related

Word Frequency strcmp working infinitely using struct array

I want to do a read word by word and compare what word with what I have in my struct array. If I don't have one, I want to add in the first empty spot.
#include <iostream>
#include <fstream>
#include <string>
#include<string.h>
using namespace std;
struct cuvinte{
char *cuvant;
int numar;
};
int main()
{
cuvinte multime[100];
ifstream f;
f.open("input.txt");
string str;
while(getline(f,str))
{
char * cuvant = new char[str.size() + 1];
char * abc = new char[str.size() + 1];
copy(str.begin(), str.end(), abc);
cuvant = strtok (abc," ,/_");
while(cuvant!=NULL)
{
for(int i=0;i<10;i++)
{
cout<<cuvant;
if(strcmp(cuvant,multime[i].cuvant)==0)
multime[i].numar++;
else
{
for(int j=0;j<10;j++)
if(multime[j].numar==0)
{
multime[j].cuvant=cuvant;
multime[j].numar=1;
}
}
}
cuvant = strtok ( NULL , " ");
}
}
return 0;
}
Strcmp works infinitely and only takes the first word; I don't know why.
In C++ it should only take a handful of lines:
#include <string>
#include <fstream>
#include <unordered_map>
using WordFrequency = std::unordered_map<std::string, unsigned>;
WordFrequency read_words(std::istream& s) {
WordFrequency wf;
for(std::string word; s >> word;)
++wf[word];
return wf;
}
int main() {
std::fstream f("input.txt");
auto wf = read_words(f);
}
Before using word you may like to lower-case it and remove all punctuation, so that your dictionary doesn't contain separate entries for the same word, e.g. Or, or, or,.

use std::vector<char> in C++/cLi [duplicate]

I have a function in C++ that have a value in std::string type and would like to convert it to String^.
void(String ^outValue)
{
std::string str("Hello World");
outValue = str;
}
From MSDN:
#include <string>
#include <iostream>
using namespace System;
using namespace std;
int main() {
string str = "test";
String^ newSystemString = gcnew String(str.c_str());
}
http://msdn.microsoft.com/en-us/library/ms235219.aspx
Googling reveals marshal_as (untested):
// marshal_as_test.cpp
// compile with: /clr
#include <stdlib.h>
#include <string>
#include <msclr\marshal_cppstd.h>
using namespace System;
using namespace msclr::interop;
int main() {
std::string message = "Test String to Marshal";
String^ result;
result = marshal_as<String^>( message );
return 0;
}
Also see Overview of Marshaling.
As far as I got it, at least the marshal_as approach (not sure about gcnew String) will lead to non ASCII UTF-8 characters in the std::string to be broken.
Based on what I've found on https://bytes.com/topic/c-sharp/answers/725734-utf-8-std-string-system-string I've build this solution which seems to work for me at least with German diacritics:
System::String^ StdStringToUTF16(std::string s)
{
cli::array<System::Byte>^ a = gcnew cli::array<System::Byte>(s.length());
int i = s.length();
while (i-- > 0)
{
a[i] = s[i];
}
return System::Text::Encoding::UTF8->GetString(a);
}

conversion string to character in c++ Not Working Properly

make a function which receive the file name but it not working properly because it receives "Doctor.txtG" but I am giving "Doctor.txt" how can i resolve it?My code is Given below......
#include <iostream>
#include <conio.h>
#include <fstream>
using namespace std;
int number_of_lines = 0;
int numberoflines(string A);
int main()
{
cout<<numberoflines("Doctor.txt");
getch();
return 0;
}
int numberoflines(string A)
{
int Len;
char Chr[Len];
Len=A.length();
A.copy(Chr, Len);
//cout<<Len;
cout<<Chr;
string line;
ifstream myfile(Chr);
if(myfile.is_open())
{
while(!myfile.eof())
{
getline(myfile,line);
number_of_lines++;
}
myfile.close();
}
return number_of_lines;
}
It needs to copy a null-terminated byte into Chr.
Use
strcpy(Chr, A.c_str());
instead of A.copy(Chr, Len);
And you should properly init Chr like
char Chr[1024]
or
char* Chr = new char[Len + 1].
Your problem is happening because you are trying to create a char array with the size Len. But you have not initialized Len before using it. This is why it is resulting in undefined behavior and creating this problem. Always try to initialize variables when you declare them. Otherwise, this problem will happen quite often.
However, You don't need to create another char array. Just use std::string::c_str(); in your parameter for the constructor of the ifstream. I am giving a sample code below. This should solve your problem.
#include <iostream>
#include <conio.h>
#include <fstream>
using namespace std;
int number_of_lines = 0;
int numberoflines(string A);
int main()
{
cout<<numberoflines("Doctor.txt");
getch();
return 0;
}
int numberoflines(string A)
{
string line;
ifstream myfile(A.c_str());
if(myfile.is_open())
{
while(!myfile.eof())
{
getline(myfile,line);
number_of_lines++;
}
myfile.close();
}
return number_of_lines;
}

c++ ofstream(someVariable) initialization

So I tried to do this:
#include <iostream>//For cout/cin
#include <fstream> //For ifstream/ofstream
using namespace std;
int main()
{
string types[] = {"Creativity", "Action", "Service"};
for(int i = 0; i < sizeof(types)/sizeof(string); i++) {
string type = types[i];
string filename = type + ".html";
ofstream newFile(filename);
//newFile << toHTML(getActivities(type));
newFile.close();
}
return 0;
}
and I'm being hit with errors. I'm new to C++, so I don't know what to try, or if this is even possible (SURELY it is...).
I tried the following, but it was really just a stab in the dark and didn't help:
#include <iostream>//For cout/cin
#include <fstream> //For ifstream/ofstream
using namespace std;
int main()
{
string types[] = {"Creativity", "Action", "Service"};
for(int i = 0; i < sizeof(types)/sizeof(string); i++) {
string type = types[i];
//Attempting to add const..
const string filename = type + ".html";
ofstream newFile(filename);
//newFile << toHTML(getActivities(type));
newFile.close();
}
return 0;
}
I mean, its all happy if I do `ofstream newFile("somefile.html");
The original IOstream library didn't have a constructor taking a std::string. The only type supported was char const*. You can get a char const* from a std::string using c_str():
std::string name("whatever");
std::ofstream out(name.c_str());
The type of a string literal isn't of type std::string but it is of type char const[n] where n is the number of characters in the string, including the terminating null character.
In C++ 2011 the File stream classes are improved to also take std::string where a string is expected.

Function to parse string with tokens

I know how to program in C# and VB but not have idea about how to use C++ and have to program a little exe to a barcode scanner that use C++ :(
In this moment I try to parse a scanned barcode that have multiple data sepparated with a "/", I find that exist a strtok function, tested it "manually" and worked ok but I not implemented yet a working function to call it correctly, what I have now:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int elemStr(char *str, char sep)
{
int cantElem;
unsigned ich;
cantElem = 0;
if (strlen(str) > 0) //at least 1 elem
cantElem++;
for (ich = 0; ich < strlen(str); ich++)
{
if (str[ich] == sep)
cantElem++;
}
return cantElem;
}
char* getElemStr(char *str, char sep[], int elem)
{
char *tempStr = NULL;
char *tok;
int currElem = 1;
// 1st data
strcpy( tempStr, str);
tok = strtok( tempStr, sep);
while( currElem != elem )
{
// Get next tokens:
tok = strtok( NULL, sep );
currElem++;
}
return tok;
}
void main( void )
{
char barcode[] = "710015733801Z/1/35";
char sep[] = "/";
char sep1 = sep[0];
char barcd[20];
char piezaChar[4];
int pieza;
char mtsChar[4];
int cantElem;
cantElem = elemStr(barcode, sep1 );
if (cantElem >= 1)
{
strcpy(barcd, getElemStr(barcode,sep,1) ); //pasa a str resultado;
printf("Cod: %s\n", barcd ); //STACK OVERFLOW HERE!
}
}
if I use strtok witout a function "getElemStr" it work ok but I try to use it on other places too.
Can I use strtok like this? You have a working example?
pd: I not have idea about pointers (sorry), good doc to learn about that?
Since you specifically asked about C++, I'm going to ignore your very c-style code and show you how to do this in C++:
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::string barcode = "710015733801Z/1/35";
std::string sep = "/";
std::vector<std::string> v;
boost::split(v, barcode, boost::is_any_of(sep));
for(size_t i=0; i<v.size(); ++i)
std::cout << v[i] << '\n';
}
strtok destroys your original string. So i don't think it can be used with a char* that points to a static string. Static strings get copied to a read only portion of the executable.
Here is a C++ solution that doesn't use boost.
#include <string>
#include <sstream>
#include <iostream>
int main()
{
std::string barcode = "710015733801Z/1/35";
std::stringstream ss(barcode);
std::string elem;
while(std::getline(ss, elem, '/'))
{
//do something else meaningful with elem
std::cout << elem << std::endl;
}
return 0;
}
Output:
710015733801Z
1
35