I am fairly new to VBA, but not to coding in general. I have an Excel worksheet, part of which I need to export into a text file. However, to ensure the escape characters don't collide with anything written in the worksheet, and also to match up with how escape characters are handled in other (Java & C) apps at our company, I would like to use the ascii character "thorn".
Some languages that I know, like perl, will simply let you enter an escape character and the ascii number of the symbol. E.g., I could type "\231" in order to get "thorn". It seems this is not supported in VBA. Or if it is, I am not sure how.
Lastly, below is a brief snippet of how I imagine I would implement the code, with just the key statements. If you have a better suggestion, or know why this wouldn't be the best way, please let me know.
Open sFullPath For Output As #1
(... some code to capture the values to be written to the next line of the file ...)
sLine = Join(sValues, <the thorn character>)
Print #1, sLine
The code above would be placed within a loop that would regenerate "sValues", the set of relevant cells in the next row to be printed to the text file.
Thanks so much!
Regards,
Mike
To print an ASCII character using its code number, use the chr() function
Extended ASCII isn't standard across all platforms. For whatever reason, the number needed is 222.
So you would need
sLine = Join(sValues, chr(222))
Print #1, sLine
Try replacing <the thorn character> with Chr(254). Let me know how that goes.
Related
Please help me. I'm new to programming. I've been trying and searching for days to answer this.
I need a C program that will open a text file named users.txt
In that text file there are names then comma and their schools.
John Paul, Legit Univ
Paul John, Yo Univ
Lebron James, School Univ
James Lebron, Legit Univ
All I managed so far is to display them all. The output should be all the users that are from "Legit Univ".
Sample output:
Found 2 users from Legit Univ
John Paul
James Lebron
Use fgets() to read a line from file into a string, then strchr() to to find the position of the comma ',' field separator in the string (or strstr() if the field separator is comma space ", "). Now you can check the part of the string after the field separator for a match on your query with strcmp(). Instead of parsing the file, you could also use a regex and match against the string.
Another approach would be to load one line at a time (as you are doing). Then, search the buffer for the target string (like "Legit Univ") using strstr(). If found, then go about trimming at the comma (perhaps strtok() and outputting the name ( puts() ). If not found, move on to the next record (input line).
Don't do more processing than you have to.
The "Found 2 users at Legit Univ" is a bit of a problem, though. Either you can buffer up the names in a linked-list while counting 'hits', OR you could change the output to print the count of 'hits' AFTER the report of the names. (Or, worse, you could process the file once to count, rewind, then output the count AHEAD of the 'user' names...) How stringent are the requirements of the output?
You can take the strings with the following:
fscanf(<stream_name>,"%[^,] %s",<string_name_1>,<string_name_2> ) here %[^,] basically means read until you see a comma and store it in <string_name_1> and store the rest in <string_name_2>. After that, all you need to do is strncmp the <string_name_2> with the name of the university you are searching for, and if the result of strncmp is zero, that means what is written and what you are searching for is the same.
I'm not an expert in bash coding and I'm trying to do one interative-like code to help me in my work.
I have a file that contains some numbers (coordinates), and I'm trying to make a code to read some specific numbers from the file and then store them in an array. Modify that array using some arithmetic operation and then replace the numbers in the original file with the modified array. So far I've done everything except replacing the numbers in the file, I tried using sed but it does not change the file. The original numbers are stored in an array called "readfile" and the new numbers are stored in an array called "d".
I'm trying to use sed in this way: sed -i 's/${readfile[$j]}/${d[$k]}/' file.txt
And I loop j and k to cover all the numbers in the arrays. Everything seems to work but the file is not being modified. After some digging, I'm noticing that sed is not reading the value of the array, but I do not know how to fix that.
Your help is really appreciated.
When a file isn't modified by sed -i, it means sed didn't find any matches to modify. Your pattern is wrong somehow.
After using " instead of ' so that the variables can actually be evaluated inside the string, look at the contents of the readfile array and check whether it actually matches the text. If it seems to match, look for special characters in the pattern, characters that would mean something specific to sed (the most common mistake is /, which will interfere with the search command).
The fix for special characters is either to (1) escape them, e.g. \/ instead of just /, or (2) (and especially for /) to use another delimiter for the search/replace command (instead of s/foo/bar/ you can use s|foo|bar| or s,foo,bar, etc - pretty much any delimiter works, so you can pick one that you know isn't in the pattern string).
If you post data samples and more of your script, we can look at where you went wrong.
I've been struggling to get this to work for a little while and debugging it has been a pain... I'm running into a few different issues.
I prompt for user input something like this:
InputBox, stuff, Enter information, Enter obfuscated information in the box below,,,,,,,30000,
dexStuff= ""
Loop, Parse, stuff
dexStuff:= AscToDec(%A_LoopField%)
The AscToDec function is painfully simple:
AscToDec(c){
return Asc(c)
}
This is ending up with dexStuff as 0000 when I enter "test" as my string. If I change the call of AscToDec() to just MsgBox %A_LoopField% it prints out t e s t in different popups.
Can someone help me understand what I'm doing wrong here?
To concatenate the converted characters of your input, use this:
InputBox, stuff, Enter information, Enter obfuscated information in the box below,,,,,,,30000,
Loop, Parse, stuff
dexStuff := dexStuff . Asc(A_LoopField)
MsgBox %dexStuff%
I see two bugs in your code. First, initializing dexStuff isn't necessary, but if you do so, use either dexStuff= or dexStuff:="", not dexStuff="" which sets dexStuff to two quotation marks. Second, you don't need to dereference A_LoopField in your function call by using percent signs.
Here is your corrected code
InputBox, stuff, Enter information, Enter obfuscated information in the box below,,,,,,,30000,
dexStuff:="" ; := operator, not =
Loop, Parse, stuff
dexStuff:= AscToDec(A_LoopField) ; A_LoopField not %A_LoopField%
I started to learn C and I am writing a code where the user needs to type a string he wants to search in the csv file , and if the string was found , it will print the number of line the string was found at .
and I am not exactly sure what is the best way to do that
You may read line by line using getline() until it returns -1 (EOF), keeping a counter on how many iterations you have done.
For each line you read, you can use strstr() to check if the string is present. If yes, just print the counter (that will correspond to the line).
SITUATION:
My instructor for my micro-controller class refuses to save sample code to a text file and instead saves it to a word document file instead. When I open up the doc file and copy/paste the code into my IDE "CodeWarrior" it causes errors upon compile time.
I am having to rewrite all the code into a text editor and then copy/paste it into my IDE.
MY UNDERSTANDING:
I was told to always save code as a text file because when you save code as a word document file it will bring in unwanted characters when your copy/pasting the code into your IDE for compiling.
MY QUESTIONS TO YOU:
1.)
Can someone explain this dilemma to me so I can understand it better? I would like to present a better case next time when I receive errors and to also know more about what is happening.
2.)
Is it possible to write a script that will show me all the characters that are being copied and pasted into a file when the code is coming from a word document vs. a text file? In otherwords is there a program that will allow me to see what is going on between copying/pasting code from a word doc file versus a txt file?
Saving source code as a Word document is just silly. If your instructor is insisting on this, chances are no matter how well-reasoned and thorough your argument, they're not going to listen. They're beyond help.
However, to answer your questions: 1) It depends on what you're pasting the thing into. Programs that copy onto the clipboard usually make the data available in several different formats, ranging from their own internal format to plain ASCII text, to maximize compatibility so that the data can be pasted into pretty much any target program. Most text editors will only accept the plan-text version, in which case no extra characters should be transferred. However if your text editor supports RTF or HTML, this may not be true. I'm not sure what CodeWarrior supports but it is certainly possible.
A workaround if this is the case: First paste into a PURE text editor like Notepad. Then copy from Notepad into CodeWarrior. This should eliminate any hidden formatting. As shoover said above, make sure double-quotes " are really double-quotes and not the fancy left- and right-specific quotes that Word sometimes uses.
Use a hex editor like XVI32 to see the raw contents of the file, including nonprinting characters. Or use a text editor with support for showing nonprinting characters (vi/vim, etc.).
I'm studying C and I've just had the same problem. When coping a piece of code from a PDF file and trying to compiling it, gcc would return a serie of errors. Reading the answer above I had an idea: "What if I converted the utf8 into ascii?". Well, I found a website that does just that (https://onlineutf8tools.com/convert-utf8-to-ascii). But instead of also converting the utf8 characters into ascii, it showed them as hexadecimals (Copying from the website to the text editor you can see it better). From there i realised that the problem were mostly the quote marks "".
I then copied the ascii "translation" into my code editor (I must add that it worked fine with Sublime, while VScode read the same utf8 code as it was in the original file, even after cp from the website) and replaced all the hex with the actual ascii characters that were needed to compile the code properly. I used the function find and replace from my editor to do it. I must say that it wasn't very fast doing it. But I believe that in some cases, if the code you're trying to copying is too long, doing it the way I've just described could be faster than rewriting the entire code.