C/C++ :: Passing String Variable To Char Array?
Mar 30, 2014
I have program where i have to check to see if file exist, if it does not then it needs to be created. SO I have a read file that works fine, if th efile exist it reads whats in it, if it does it says the file does not exist. Now Im trying to creata function that creates the file if it doesnt exist. so in my read function when the person enters the name of the file to be checked for..I pass that name to a variable called name..Hoping that I could then pass it into my create file function if it does not exist..and use that variable to pass the name they entered into the createfile array..called filename.. but I am having trouble because i get error when i try to pass from a string name to char array.. even when I change the varialbe name to char, or char [256] it will not work.. I try to fing a way to convert th string to a char using the strncopy function but still no dice..here the code i have for the createfile funciton
void CreateNew(ofstream & FileNew)//Create file
{
char filename[256];
string name;
[Code].....
How can i do this without having to ask the person to enter the file name twice..
View 12 Replies
ADVERTISEMENT
Oct 10, 2013
I have been assigned the following task and I am having difficulty in getting it to compile. The compiler is stopping at line 27 but I don't no what the error is.
The task is as follows:
Write two functions with the following function prototypes:
int my string len(char str[])
void my string cat(char dest[], char src[], int dest size)
Both functions should take zero-terminated strings as input. The first function should return the length of the string to the calling function. The second function should take a source and a destination string and the total size of the array pointed to by dest. It should then concatenated the source string onto the end of the destination string, if and only if the destination string has the capacity to store both strings. If the destination string does not have the capacity it should not alter either, print and error, and return to the calling function. Demonstrate the use both the above functions by using them in a program in which you initialise two character arrays with two strings, print out their length, print out the combined length, and print out the combined string
And this is my code so far:
/* A program to demonstrate string concatenation */
#include <iostream>
#include <string.h>
using namespace std;
int my_string_len(char str[]){ // function to calculate length of a chracter array
int len = 0;
[Code] ....
View 8 Replies
View Related
Sep 8, 2014
#include<iostream>
using namespace std;
int main(){
int size_col = 12;
int size_row = 12;
char months_array[12][12] =
[Code] ....
I am having problems with passing a block of a char to a variable, so i could print it out.
It gives me an error: value of type *char cannot be assigned to a entity type char.
View 7 Replies
View Related
Nov 23, 2013
In the below program, When the getline function is called, it passes a char array of size 1000 by VALUE. It must have passed by value because there is no pointer or reference in the argument list of the getline function definition. And if that's the case, when exiting the getline function, isn't the s[] char array destroyed? And if it is destroyed, then when we reference line in the copy function, what are we actually copying?
#include <stdio.h>
#define MAXLINE 1000/* maximum input line length */
int getline(char line[], int maxline);
void copy(char to[], char from[]);
/* print the longest input line */
main() {
[Code] .....
View 3 Replies
View Related
Dec 23, 2013
I am trying to write a light weight printf style function.
I have got this far:
Code:
void println(const char *txData){
LOG(__PRETTY_FUNCTION__);
UARTPuts (LPC_UART0, txData);
}
void miniPrint(const char *format, ...)
{
unsigned int index = 0;
va_list argptr;
va_start(argptr, format);
[Code]....
I understand why I think. When I am passing the reference to the array possion it is outputting everything up to the next /0. So my question is how do I stop it?
I dont have much choice as to how the output wants it:
Code: void UARTPuts(LPC_UART_TypeDef *UARTx, const void *str)
Thats library code, so I dont want to change it. I.e I have to pass an address into println.
View 1 Replies
View Related
Mar 3, 2014
I am trying to set a variable of type char equal to an element in an array of characters. For example:
char data[4] = "x+1";
char element;
element = data[2];
This seems like a logical progression from number arrays, but when I print both element and data[2], I get data[2] as expected, but element gives a different character every time (I assume a garbage value).
View 5 Replies
View Related
Apr 20, 2013
I'm trying to "tokenize" a string using std::string functions, but I stored the text in a char array. When I try to convert it to a string, the string has the first character right but the rest is garbage.
// Get value from ListBox.
char selectedValue[256];
memset(selectedValue, NULL, 256);
SendMessage(GetDlgItem(hWnd, IDC_LB_CURRENTSCRIPT), LB_GETTEXT, selectedIndex, (LPARAM)selectedValue);
// Convert to string.
string val(selectedValue);
[Code] ....
View 3 Replies
View Related
Oct 19, 2013
I have some code:
char cHomeTeamFaceOffsPercentageWon[100];
memcpy(cHomeTeamFaceOffsPercentageWon,cSqlHomeTeamFaceOffsPercentageWon,100);
After this, for example, cHomeTeamFaceOffsPercentageWon is, "29%".
Then, I use
std::string szwPercentageWon = std::string(cHomeTeamFaceOffsPercentageWon);
szwPercentageWon is then, "2". Shouldn't this convert correctly, to "29%" as well.
Or is there something I'm missing? Maybe the termination character, or something.
View 1 Replies
View Related
May 10, 2013
I would like to convert a float or double variable into unsigned char variable and back.
float number_float = 23.453f;
unsigned char* number_char = (unsigned char*)malloc(sizeof(float));
number_char = reinterpret_cast<unsigned char*> (&number_float);
float* number_float_0 = reinterpret_cast<float*>(&number_char);
I am not getting the same value back.. why?
View 2 Replies
View Related
Mar 30, 2014
In this program, I have to ask the user for an employee, then the program will check to see if the file for that employee exist, if it doesnt then it will automatically create the file.
ReadNew function reads the file....check to see if it exist
CreateNew function creates a new file.
In my code I have no problem with the first part of reading file.. and my createnew function works in other programs where I am asking for input of file name to create the file name. However in this code I cannot figure how to automatically pass the input filename from the ReadNew function to the CreateNew function. I can't ask the user to enter the name a second time, so I have to pass the input filename into both functions. Here is my code.
Code:
//Create a file, append to it, and read it.
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
using namespace std;
char filename[256];
string n;
string filelist;
void CreateNew(ofstream & FileNew);
[Code]...
View 1 Replies
View Related
Oct 15, 2013
Currently I have:
Code:
char my_array[4] = { '1', '2', '3', '4' };
how do I convert the above to a string of: "1234"
View 6 Replies
View Related
Mar 8, 2013
.I have this string that I need to convert into a 2d char array like this:
String str= "A,E,B,I,M,Y#N,R,C,A,T,S";
I know how to use delimiter and split string and I know how to convert but only from string to char[].I need something like this:
Input: String str= "A,E,B,I,M,Y#N,R,C,A,T,S";
Output: [A] [E] [B] [I] [M] [Y][N] [R] [C] [A] [T] [S]
View 6 Replies
View Related
Jan 31, 2015
Assign value of pow(2,800) to char array or string ....
View 1 Replies
View Related
Apr 3, 2013
I have a text file with state names, and state abbreviations, thusly:
ALASKA
AK
ARKANSAS
AR
..and so on.
I have to load the abbreviations ONLY from the file into an array of char[ - (already done and tested).
I have to get a 2 char abbreviation as a string,then test it against the state array to make sure it is a valid abbreviation. As it stands, my test is never finding an invalid abbreviation..
Here is where I get the input:
void getState() {
char state[10];
getString("Please enter the state as a 2 char abbreviation:",state,10);
printf("State Entered:%s", state);
validState(state);
[Code] ....
View 14 Replies
View Related
Apr 29, 2015
How do I convert a string of unknown length to a char array? I am reading strings from a file and checking if the string is a Palindrome or not. I think my palindrome function is correct, but do I do something like char [] array = string.length(); ??
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
[Code].....
View 2 Replies
View Related
Aug 12, 2013
I'm new in the C programming language, so I tried to create a program that reverses a string. This is my code:
/* Reverse string */
#include <stdio.h>
#include <string.h>
int main() {
char s[8]="Welcome";
[Code] ....
The output of the program is "@".
View 5 Replies
View Related
Dec 16, 2013
why does this give me an error when i try to change the elements of the char string array.
code:
int main(void)
{
char *name = "aaa";
// setup the name
name[0] = 'Z';
name[1] = 'e';
name[2] = 'd';
name[3] = '';
return 0;
}
[code]....
View 3 Replies
View Related
May 6, 2013
#include <iostream>
using namespace std;
struct box{
[Code].....
C++Dev.cpp:23: error: incompatible types in assignment of ‘const char [15]’ to ‘char [40]’
View 2 Replies
View Related
Jan 26, 2014
I am new to C++ and I have a two player word guessing game working well. However, I would like to be able to validate whether the word entered by player 1 is a completely alphabetic word using isalpha.
The error I am getting right now is as follows:
"error: array must be initialized with a brace-enclosed initializer
char str[100]=hiddenwordtwo;"
/* isalpha portion of code */
#include <stdio.h>
#include <ctype.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>
using namespace std;
int main () {
char hiddenwordtwo[100];
[Code] .....
View 2 Replies
View Related
Apr 25, 2014
I am trying to store each char of a string(string a ="1100") into a byteArray ( byte[] byteArray = new byte[4]. its not showing any error but its storing like below:
byteArray[0] = 49
byteArray[1] = 49
byteArray[2] = 48
byteArray[3] = 48
and what i want is
byteArray[0] = 1
byteArray[1] = 1
byteArray[2] = 0
byteArray[3] = 0
I don't know why but its replacing 1 with 49 and 0 with 48.what am I doing wrong or how to do this?
my code is as below
byte[] byteArray = new byte[4];)/>
int binArrayAdd = 0;
string a ="1100";
foreach (char Character in a)
{
byteArray [binArrayAdd] = Convert.ToByte(Character);
binArrayAdd++;
}
View 4 Replies
View Related
Sep 24, 2014
I want to take string input into a char array.
What is the functionality for the above problem.
View 1 Replies
View Related
Feb 22, 2013
I have this function in a class: and a private declaration: how can I copy the parameter "ProductName" to allowedProductName. I tried all combination and I can't get it to compile.
private:
StatusPanel &statusPanel;
char allowedProductName[MAX_NAME_LENGTH];
[Code].....
View 9 Replies
View Related
Apr 17, 2014
STRING s1 = “FOOBAR”
Here STRING is a user defined class. how to assign a constant char array "FOOBAR" to string object? Copy constructor need to be same class type as parameter. and overloading assignment operator also need to be same class type. I think 'friend' can let pass another type of object rather than STRING to do operation on overloaded '=' operator. How could it be done if it is possible at all?
View 2 Replies
View Related
Dec 3, 2014
how can i compare an element of the char array and string with single chsracter also how to compare char array to cpp string
View 3 Replies
View Related
Feb 17, 2014
This code ran well until i added in the ToLower function which is supposed to convert the char array string to lower case (based off ascii strategy -32). correct this function so it converts string to lower case and doesn't get errors.
#include <iostream>
#include <string>
using namespace std;
const int MAX = 81; //max char is sting is 80
void ToLower(char s[]);
int main(){
string y_n;
[Code]...
View 1 Replies
View Related
Feb 11, 2014
I am trying to read a tab delimited file containing 8 columns and store each element of the column as an 1-dimensional array. Though the program prints the strings correctly as in the commented printf but it doesn't give me first_col[0] value and so on. My code is as follows:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main() {
FILE *int_file1;
int BUF = 1024;
[Code]...
The inputfile test.txt has the following elements:
34932 13854 13854 2012-01-07
172098 49418 53269 2012-01-07
-
-
-
View 7 Replies
View Related