FAQ Database Discussion Community


ifstream no conversion from char to char exists error

c++,char,ifstream,getline,c-strings
int main() { char buffer[1024]; ifstream dataFile ("./data.dat"); while(buffer) { localHouse->location = dataFile.getline(buffer, 1024); } } This throws the error: No suitable converion function from "std::basic_istream<char, std::char_traits<char>>" to "char" exists. It continues to throw this error if I use a pointer to buffer instead. as far as I can...

Copy vector into char*

c++,vector,char,c-strings
I'm just studying C and C++ programming. I've searched and can't seem to find an answer that has a decent response. Of course using <string> is much easier but for this task I am REQUIRED to use only clib <string.h> functions; I'm also not allowed to use C++11 functions. I...

Converting form CString to const char*

c++,const,c-strings
I've wrote a method that convert form CString to const char *: const char* CAESDlg::ConvertToChar(CString str) { CStringA charstr(str); const char *cstr = (const char *)charstr; return cstr; } but when trying to put it in a const char * it doesn't return the correct value! const char *test =...

C character array and its length

c,c-strings
I am studying now C with "C Programming Absolute Beginner's Guide" (3rd Edition) and there was written that all character arrays should have a size equal to the string length + 1 (which is string-termination zero length). But this code: #include <stdio.h> main() { char name[4] = "Givi"; printf("%s\n",name); return...

how do I delete allocated memory and still return its value from method

c++,dynamic-arrays,c-strings
I have this function as part of a class I am writing: const char* sockets::TCPSocket::doRecv(int flags) { char* incomingDataBuffer = new char [this->bufferSize]; ssize_t bytesReceived = recv(this->filedes, incomingDataBuffer, this->bufferSize, flags); // TODO set timeout - If no data arrives, the program will just wait here until some data arrives. if...

Scan and Print a string

c,c-strings
Ive written a very basic code and cant seem to get it to work. char *a; scanf("%s",a); printf("%s",a); Input: aWord Output: Null Where am i wrong here?Why am i getting null output? I tried doing the following and it works as expected, why is it the case? char a[50]; scanf("%s",a);...

C program Strings Example how come the result is 98?

c,string,dynamic-arrays,c-strings
C program Strings Example how come the result is 98? #include <stdio.h> int main() { char s[]="%d%d%d"; int a=9,b=8,c=5; printf(s+2,a,b,c); return 0; } ...

fstream . Trouble printing all items in list

c++,file,menu,fstream,c-strings
I'm working on a menu driven program that has users basically keep track of their assignment tasks and due dates. My program deals with a text file titled "tasks.txt", and the user has 3 options for interacting with the text file: Enter a new task, Display all the tasks in...

static_cast from 'const char *' to 'void *' is not allowed

c++,casting,c-strings
In C++, I'm trying to print the address of a C-string but there seems to be some problem with my cast. I copied the code from a book but it just doesn't compile on my mac. const char *const word = "hello"; cout << word << endl; // Prints "hello"...

strcpy adding random numbers to empty string

c,string,strcpy,c-strings
I am trying to flush out a char buffer by using strcpy(buffer, ""). strcpy() seems to be putting random numbers in the string. In gdb, I see the buffers (received_message): Before strcpy() call: (gdb) print received_message $6 = "9210070627\000\000\000\000\000\000\000\000\000" After strcpy() call: (gdb) print received_message $8 = "\000\062\061\060\060\067\060\066\062\067\000\000\000\000\000\000\000\000\000" Where \060...

How do I output a bubble sorted 2-D string array in C?

c,arrays,computer-science,bubble-sort,c-strings
How do I go about sorting a 2-D string array in C using bubble sort (or any other kind of sorting in that matter) ? What I'm actlly trying to do is as follows : Example: Unsorted 2-D string array : abfg abcd xyzw pqrs orde Sorted 2-D string array:...

Bug (??) in formatting long values with CString::Format

c++,format,long-integer,c-strings
I have issues with the following code: CString cstr; cstr.Format("SELECT foobar %ld, %ld ", " AND type = '%s' ", 1426233870,1426243870, "'OR'"); The resulting CString contains wrong numbers! The code is simplified, actually I am selecting datasets from a database between two timestamps in seconds. I was able to solve...

Implementing `strtok` whose delimiter has more than one character

c,delimiter,strtok,c-strings
Code snippet: char str[] = "String1::String2:String3:String4::String5"; char *deli = "::"; char *token = strtok(str,deli); while(token != NULL) { printf("Token= \"%s\"\n", token); token=strtok(NULL,deli); } The above code snippet produces the output: Token="String1" Token="String2" Token="String3" Token="String4" Token="String5" but I want the output to be: Token="String1" Token="String2:String3:String4" Token="String5" I know that I am...

Manipulation of arrays of strings in C

c,arrays,c-strings
I'm working on a basic shell (as in the console program that awaits commands and executes them in UNIX systems) replica in C, and need to be able to manipulate 2d arrays of char to store the environment variables. I wrote a small function to create that 2d array and...

Casting literals to PChar / PAnsiChar

delphi,access-violation,c-strings
I've got really stupid question... Why this code: PChar('x'); causes "Access violation" error? Compiler optimalisation? Example: var s: String; ... s := StrPas(PAnsiChar('x')); This causes AV in Delphi 5 / Delphi XE Or this one: Windows.MessageBox(0, PChar('x'), PChar('y'), 0); This causes AV in Delphi 5, but not in Delphi XE...

Conflicting types for 'memchr' [closed]

c++,c,c++builder,c-strings,c++builder-xe6
I'm currently trying to modify a library (ASN.1 Compiler), written in C, so I can compile and use it in C++ Builder (XE6). Doing that, I've encountered the error "Conflicting types for 'memchr'" (in cstring). After some research, it seems that this problem comes from the fact that C and...

c strings unable to display (and maybe input)

c++,c-strings
I am stuck at this part of the code. The problem here is that the display from the console always cut off when I have yet to finish my input. Here is the code: else if (m == 'c') { std::cout << "Enter the number of sentences used: "; int...

CStrings and pointers: Heap corruption when trying to delete a character array

c++,arrays,pointers,c-strings
I've exhausted myself googling this, but I haven't been able to find a clear answer or help myself understand what is going wrong. As part of a homework assignment, I'm trying to dynamically allocate memory for a character array (ie. a CString) and assign a char pointer variable to point...

Converting long double to CString

c++,mfc,c-strings,long-double
I am working on C++ MFC project build in unicode settings and I usually use function _ttof to convert CString to double but i couldn't find a function for long double which use TCHAR. Thanks in advance....

Is it possible to allocate the correct amount of space to strings in C during run time?

c,char,runtime,c-strings,dynamic-allocation
Is there any way to allocate exactly enough space during runtime WITHOUT asking the length of the string? int main() { char **tests; int counter; int i; int j; int testCases; scanf(" %d", &testCases); tests = malloc(sizeof(char *) * testCases); for(i = 0; i < testCases; i++) //stuck here, normally...

How to read string until two consecutive spaces?

c,format,sscanf,c-strings
A well known function of the scanf() functions is that you can pass a format to scan input according to this format. For my case, I cannot seem to find a solution searching this and this documentation. I have a string (sInput) as the following: #something VAR1 this is a...

Convert C# String to MFC CString in C# project?

c#,c++,string,mfc,c-strings
I am developing a client in C#. The server was developed by other who used C++ MFC, so I can´t change it. The server can only accept string data as CString (UTF8). Note: before I ask this question I have searched and read many threads, eg. thread1, thread2, thread3, etc....

CString to LPCTSTR conversion breaks sql query

c++,atl,c-strings
I have a part of larger sql query stored as LPCTSTR LPCTSTR var = _T(" SELECT ..."); and it works fine. I need to modify the query with a variable, so I'm trying CString sqlQuerry = _T(" SELECT ... %s"); CString sqlString, stringVar = _T("variable"); sqlString.Format(sqlQuerry, stringVar); var = (LPCTSTR)sqlString;...

Mallocing Space for String Array in C

c,arrays,pointers,return-type,c-strings
I need help malloc space for a string array and then returning this value as a pointer. How do I go about doing this? My code is: char * strArray[*arrLen] = malloc(srcStrLen - numOfDelimsInStr + (numOfDelimsInStr + 1)); I populate string array with memcpy and manual '\0' adds. Then I...

Can a C implementation use length-prefixed-strings “under the hood”?

c,compiler-construction,compiler-optimization,c-strings,null-terminated
After reading this question: What are the problems of a zero-terminated string that length-prefixed strings overcome? I started to wonder, what exactly is stopping a C implementation from allocating a few extra bytes for any char or wchar_t array allocated on the stack or heap and using them as a...

Chop up string into chunks of 512 bytes in c

c,c-strings
Im trying to write a function that takes a string and chops it up into chunks of 512 bytes. It takes the first 512 bytes in the string, and stores it in chopped[0] and then the next 512 in chopped[1]...etc. It seems to be working when I print out chopped...

C - understanding struct members vs pointers (char *)

c,pointers,struct,variable-assignment,c-strings
So I started learning C. It has been smooth so far, but I am finding it difficult understanding a few things. Since I am new, I don't really know what search terms to use, but all my searches have turned up nothing useful. Look at the code below #include <stdio.h>...

How to print duplicate letters in a string with their count number in C?

c,c-strings
I have been implemnting an algorithim for counting and printing duplicate letters (two times or more) in a C string. Example : If input string was : "Hello There" The output should be : e - 3 h - 2 l - 2 My current code has been printing what...