int DownloadFtpDirectory(TCHAR* DirPath) {
WIN32_FIND_DATA FileData;
UINT a;
TCHAR* APP_NAME = TEXT("ftpcli");
TCHAR* f;
int j = 5;
do {
j++;
f = _tcsninc(DirPath, j);
}while (_tcsncmp(f, TEXT("/"), 1));
TCHAR* PATH_FTP = wcsncpy(new TCHAR[j], DirPath, j);
After the last line gets a string in which there is no line ending character, how to fix this?
P.S. how to do so would be out of line "ftp://ftp.microsoft.com/bussys/", get a string ftp.microsoft.com if both strings are TCHAR ?
TCHAR* PATH_FTP = wcsncpy(new TCHAR[j+1], DirPath, j);
PATH_FTP[j] = TEXT('\0');
Related
I am doing the CS50 course and am on week 2. One of the problems of week 2 is called "Caesar". Essentially you have to write code which cyphers text by shifting letters that use the users inputted preferred number. After running my code I keep getting this error
"error: implicitly declaring library function 'strlen' with
type 'unsigned long (const char *)'
[-Werror,-Wimplicit-function-declaration]
for(i = 0, l = strlen(text); i < n; i++)"
This is the code:
int main(int argc, string argv[])
{
string n = argv[1];
int y = argc;
int key = get_int("./caesar ");//getting the number from the user
int k = (key);//assigning key a variable name.
string text = get_string("plaintext: ");//letting the user input their text.
if (key < 1)//Trying to make limit for acceptable input.
{
printf("ERROR");
return 1;
}
int l;
int i;
//for loop containing the encipher process
for(i = 0, l = strlen(text); i < n; i++)
{
if(isalpha(i))
{
if (isupper[i])
{
printf("ciphertext: %c",(text[i] + k)%26 + 65);
}
else (islower[i])
{
printf("ciphertext: %c",(text[i] + k)%26 + 65);
}
}
}
printf("ciphertext: %c", d || c);
return;
int checking_key(int y,string n)
int num = argc;
string key = y;
int num_key = atoi(key);
if(argc != 2)
{
return 0;
}
else
{
if (num_key > 0)
{
return num_key;
}
else
{
return 0;
}
}
}
From man strlen:
Synopsis
#include <string.h>
size_t strlen(const char *s);
Just like one needs to "include" cs50.h to use any of the get_* functions, string.h must be "include"d to access its functions, eg strlen.
Additionally (per comments):
The "ordered comparison" in the compile error
ordered comparison between pointer and integer ('int' and 'string' (aka 'char *')) [-Werror] for(i = 0, l = strlen(text); i < n; i++)
is i < n. Error says one of them is an int and one of them is a string.
On closer inspection this program is a long way from a clean compile. Recommend you follow along with the spec and "approach this problem one step at a time"
I have a sample code like this:
CString A = _T("abc");
CString B = _T("xyz");
CString res;
now to concat these two above strings, which one should I prefer:
res = A + _T(" ") + B;
or
res.Format(_T("%s %s"), A, B);
Made a sample function and ran the the bellow code
CString str1 = _T("abc");
CString str2 = _T("xyz");
CString strRes;
DWORD dwTickCount = GetTickCount();
const int LoopCount = 1000000;
for(int nIdx = 0; nIdx < LoopCount; nIdx++)
{
strRes = str1 + _T(" ") + str2;
}
dwTickCount = GetTickCount() - dwTickCount;
The value of dwTickCount was 2656. But while checking the second case,
CString str1 = _T("abc");
CString str2 = _T("xyz");
CString strRes;
DWORD dwTickCount = GetTickCount();
const int LoopCount = 1000000;
for(int nIdx = 0; nIdx < LoopCount; nIdx++)
{
strRes.Format(_T("%s %s"), str1, str2);
}
dwTickCount = GetTickCount() - dwTickCount;
The value of dwTickCount was 453 only.
Hence I can say, the Format approach is about 6 times faster.
int file_write(struct m_inode * inode, struct file * filp, char * buf, int count){
off_t pos;
int block,c;
struct buffer_head * bh;
char * p;
int i=0;
.......
if (!(filp->f_flags & O_APPEND)) {
filp->f_pos = pos;
inode->i_ctime = CURRENT_TIME;
}
}
I think:
int file_write(struct m_inode * inode, struct file * filp, char * buf, int count){
off_t pos, tmp;
int block,c;
struct buffer_head * bh;
char * p;
int i=0;
if (filp->f_flags & O_APPEND)
pos = inode->i_size;
else
pos = filp->f_pos;
tmp = pos;
.......
if (!(filp->f_flags & O_APPEND)) {
filp->f_pos = tmp;
inode->i_ctime = CURRENT_TIME;
}
}
otherwise int the file_read filp->pos is error.
please Verify my ideas!
Thank you!
As far as I understand, if opened as O_APPEND, file_write always writes to the end of the file (so at offset i_size) and updates i_size. And if not opened as O_APPEND, file_write writes to wherever f_pos is, and updates f_pos.
I think you expect file_write when not opened as O_APPEND to keep f_pos constant, which is not how it works I think.
In a previous code of mine, I was using the following line of code to get the last 9digits of the "command" string
if(command.indexOf("kitchen light: set top color") >=0)
{OnColorValueRed = (command.charAt(28)- 48)*100 + (command.charAt(29)- 48)*10 + (command.charAt(30)- 48);}
Now i am using a char buffer (char packetBuffer[UDP_TX_PACKET_MAX_SIZE];) and using the above code does not work since packetBuffer is not a string, how could I please go about this
Try defining a function to search the string
int indexOf_for_char(const char *str, int str_length, const char *target) {
// naive method
for (int index = 0; index < str_length; index++) {
int j;
// check if matched
for (j = 0; target[j] != '\0' && index + j < str_length && str[index + j] == target[j]; j++);
// if matched, return the index
if (target[j] == '\0') return index;
}
return -1;
}
and using subscripting.
if(indexOf_for_char(packetBuffer, UDP_TX_PACKET_MAX_SIZE, "kitchen light: set top color") >=0)
{OnColorValueRed = (packetBuffer[28]- 48)*100 + (packetBuffer[29]- 48)*10 + (packetBuffer[30]- 48);}
I have a string:
string mystring="part1, part2, part3, part4, part5";
How can I just return the first 3 elements without splitting them up first?
so like this:
string newstring="part1, part2, part3";
You could get the first three using:
RegEx r = new RegEx(#"(\S+, \S+, \S+), \S+");
I'm sure there is a better way to write the regex, but I think that would do it for basic inputs.
Try to find Index of 3rd Comma, and then get the substring.
Example
void Main()
{
string mystring="part1, part2, part3, part4, part5";
int thirdCommaIndex = IndexOf(mystring, ',', 3);
var substring = mystring.Substring(0,thirdCommaIndex-1);
Console.WriteLine(substring);
}
int IndexOf(string s, char c, int n)
{
int index = 0;
int count = 0;
foreach(char ch in s)
{
index++;
if (ch == c)
count++;
if (count == n )
break;
}
if (count == 0) index = -1;
return index;
}
This will parse the string trying to find the third comma and throwing it and everything after it away.
string mystring = "part1, part2, part3, part4, part5";
UInt16 CommasFound = 0;
UInt16 Location = 0;
for (Location = 0; (CommasFound < 3) &&
(Location < mystring.Count()); Location++)
if (mystring[Location].Equals(','))
CommasFound++;
if (CommasFound == 3)
{
string newstring = mystring.Substring(0, Location-1);
}
else { // Handle the case where there isn't a third item
}