Can somebody explain me please why FreeImage library is not recognizing my variable as a valid filename for the method Load, I tried the following code:
var fileName = "C:\\images\\myimage.tif";
var dib = FreeImage.Load(FREE_IMAGE_FORMAT.FIF_TIFF, fileName, 0);
And it's not working, the object dib is always empty (the image is not being loaded), but when I tried the following code:
const string fileName = "C:\\images\\myimage.tif";
var dib = FreeImage.Load(FREE_IMAGE_FORMAT.FIF_TIFF, fileName, 0);
The result is successful, the problem is that I need that image path value to be a normal variable (NOT const), because I work with different images each time, and this images could be anything.
How could I solve this issue, or it's a limitation of the library?
Thanks.
The first thing I see, you are setting the first example as a var instead of string. Define your variable as a string.
I would try that, you dont need the const to make it work I don't believe.
string fileName = "C:\\images\\myimage.tif";
Related
So in node.js I a have read a simple txt file into a variable, but when I want to call whichever string function like split or indexOf on the variable, it doesnt work.
if I do string.indexOf('whatever character'), I become always -1
And if i do string.splice('whichever separator') I become always this on the console: '\u0000s\u0000\r\u0000\n\u0000....'
the code:
const fs = require('fs');
let emails = fs.readFileSync('../model/hotel-emails-with-links-exerc.txt', 'utf-8');
console.log(emails.split('/'));
console.log(emails.indexOf('http'));
this is the content of the file I read : 'youreamil#gmail.com|https://www.hotel-monte.at/de/zimmer-preise/pauschalen.html?offer=27'
If I do console.log(typeof emails) I get 'string' as normal.
However string variables whiches value was not imported from a txt file are working fine.
thanks for the suggestions
In sourceConfigPath variable, it has a path like "conf/test.json", or it might have another layer, like "test/conf/test.json". I want to get the "test.json" part only.
I tried the indexOf function to get the position, then use either slice or substr function to get the 'test.json' part. But it always return 0 when do indexOf.
Can anyone please help here? many thanks!
var position = sourceConfigPath.indexOf('conf');
var newsourceConfigPath = sourceConfigPath.slice(position+4);
Or is there any better way to do this? Many thanks!
The best way is to use path.basename
The path.basename() methods returns the last portion of a path,
similar to the Unix basename
const path = require('path');
const newSource = path.basename('conf/test.json'); // test.json
You can use lastIndexOf instead of indexOf, but path.basename is recommended.
const filepath = '/path/to/file.json';
const position = filepath.lastIndexOf('/') + 1; // +1 is to remove '/'
console.log(filepath.substr(position));
I am using a library which on call of a function returns the toString of a buffer.
The exact code is
return Buffer.concat(stdOut).toString('utf-8');
But I don't want string version of it.
I just want the buffer
So how to convert string back to buffer.
Something like if
var bufStr = Buffer.concat(stdOut).toString('utf-8');
//convert bufStr back to only Buffer.concat(stdOut).
How to do this?
I tried doing
var buf = Buffer.from(bufStr, 'utf-8');
But it throws utf-8 is not a function.
When I do
var buf = Buffer.from(bufStr);
It throws TypeError : this is not a typed array.
Thanks
You can do:
var buf = Buffer.from(bufStr, 'utf8');
But this is a bit silly, so another suggestion would be to copy the minimal amount of code out of the called function to allow yourself access to the original buffer. This might be quite easy or fairly difficult depending on the details of that library.
You can use Buffer.from() to convert a string to buffer. More information on this can be found here
var buf = Buffer.from('some string', 'encoding');
for example
var buf = Buffer.from(bStr, 'utf-8');
Note: Just reposting John Zwinck's comment as answer.
One issue might be that you are using a older version of Node (for the moment, I cannot upgrade, codebase struck with v4.3.1). Simple solution here is, using the deprecated way:
new Buffer(bufferStr)
Note #2: This is for people struck in older version, for whom Buffer.from does not work
This is working for me, you might change your code like this
var responseData=x.toString();
to
var responseData=x.toString("binary");
and finally
response.write(new Buffer(toTransmit, "binary"));
Hey what's up everyone?
I have a simple app to display the file size of files selected by users. The problem is that when the file is located within a folder that has spaces in the folder name or special characters like accents (á é ç) the file path uses different symbols and then I can't get the file size.
The folder is: /Users/home/Checking/First Box
This is my code and the output:
var path = url.absoluteString?.stringByReplacingOccurrencesOfString("file://", withString: "", options: nil, range: nil)
var filePath = path
var attr:NSDictionary? = NSFileManager.defaultManager().attributesOfItemAtPath(filePath!, error: nil)
if let _attr = attr {
fileSize = _attr.fileSize();
}
println(fileSize)
println(filePath!)
And the output is nil for the file size and /Users/home/Checking/First%20Box for the file path.
Also if I select a folder with accent on its name like /Users/home/Checking/Café the output for the path will be /Users/home/Checking/Cafe%CC%81
One solution, which is the one I am using using stringByReplacingOccurrencesOfString but this adds a lot of line to the code since there are many characters with accent throughout the languages around the world and I was wondering if there's another more simple way to do this.
Any help will be much appreciated.
The problem is that you are using a wrong (and too complicated) method
to convert a file URL to a path.
Example:
let url = NSURL(fileURLWithPath: "/ä/ö/ü")!
println(url.absoluteString!) // file:///a%CC%88/o%CC%88/u%CC%88
println(url.path!) // /ä/ö/ü
As you see, .absoluteString returns a HTML string with percent escapes,
whereas .path returns the file path. So your code should look like this:
if let filePath = url.path {
println(filePath)
if let attr : NSDictionary = NSFileManager.defaultManager().attributesOfItemAtPath(filePath, error: nil) {
let fileSize = attr.fileSize();
println(fileSize)
// ...
}
}
string exeFile = (new System.Uri(Assembly.GetEntryAssembly().CodeBase)).AbsolutePath;
string exeDir = Path.GetDirectoryName(exeFile);
string fileName = Path.Combine(exeDir, #"..\..\xml\SalesOrderXMLData.csv.xml");
Hello,
The above code works if the project is in, for example,
C:\Code\
but not if its in
C:\Documents and Settings\Naim\My Documents..
If i have the string, i would use escape characters where needed, but in this case, i dont know how to get around this.
Update: result fileName = "D:\Naim\My%20Documents\Visual%20Studio%202008\Projects\XML_Gen\XML_Gen\bin\Debug\..\..\xml\SalesOrderXMLData.csv.xml"
Any help appreciated.
Thanks
It's probably the URI. Use Assembly.GetEntryAssembly().Location, and pass it directly to Path.GetDirectoryName().
code below works, though I don't know why the above wasn't working. Changed AbsolutePath to LocalPath
string exeFile = (new System.Uri(Assembly.GetEntryAssembly().CodeBase)).LocalPath;
string exeDir = Path.GetDirectoryName(exeFile);
string fileName = Path.Combine(exeDir, #"..\..\xml\SalesOrderXMLData.csv.xml");