c-strings

How to dynamically allocate memory for char** in C

时光总嘲笑我的痴心妄想 提交于 2020-01-01 18:19:03
问题 How would I go about dynamically allocating memory to char** list in this function? Basically the idea of this program is I have to read in a list of words from a file. I cannot assume max strings or max string length. I have to do other stuff with the C-strings but that stuff I should be fine with. Thanks! void readFileAndReplace(int argc, char** argv) { FILE *myFile; char** list; char c; int wordLine = 0, counter = 0, i; int maxNumberOfChars = 0, numberOfLines = 0, numberOfChars = 0; myFile

How to dynamically allocate memory for char** in C

浪尽此生 提交于 2020-01-01 18:18:33
问题 How would I go about dynamically allocating memory to char** list in this function? Basically the idea of this program is I have to read in a list of words from a file. I cannot assume max strings or max string length. I have to do other stuff with the C-strings but that stuff I should be fine with. Thanks! void readFileAndReplace(int argc, char** argv) { FILE *myFile; char** list; char c; int wordLine = 0, counter = 0, i; int maxNumberOfChars = 0, numberOfLines = 0, numberOfChars = 0; myFile

How to use fgets to read a file line by line

淺唱寂寞╮ 提交于 2019-12-31 05:10:19
问题 I'm new at programming so there are some basics and maybe common sense that I don't know. I have a question about how to use fgets right. Based on the explanation of fgets, it seems that fgets should stop whenever it reads n-1 characters, hit the EOF or hit a newline character. For example, I create a text file like below: red 100 yellow 400 blue 300 green 500 purple 1000 ... The color and the integer is separated by a tab. When I create this text file, I need to hit enter at the end of each

fwrite in C giving different values in output files

ぐ巨炮叔叔 提交于 2019-12-31 04:21:38
问题 why are the output files different when I use fwrite in another function VERSUS fwrite in the same function? output1.txt contains garbage value like Ê, which is NOT correct output2.txt contains "b", which is correct #include <stdio.h> #include <string.h> void writeData(char *buf, char *path) { FILE *fp1; fp1 = fopen(path, "a"); fwrite(&buf, sizeof(char), strlen(buf), fp1); } int main () { char buf[2] = "a"; char buf2[3] = "yb"; char file1_path[12] = "output1.txt"; char file2_path[12] =

How should character arrays be used as strings?

大兔子大兔子 提交于 2019-12-29 07:35:13
问题 I understand that strings in C are just character arrays. So I tried the following code, but it gives strange results, such as garbage output or program crashes: #include <stdio.h> int main (void) { char str [5] = "hello"; puts(str); } Why doesn't this work? It compiles cleanly with gcc -std=c17 -pedantic-errors -Wall -Wextra . Note: This post is meant to be used as a canonical FAQ for problems stemming from a failure to allocate room for a NUL terminator when declaring a string. 回答1: A C

Strip first and last character from C string

假装没事ソ 提交于 2019-12-28 12:18:11
问题 I have a C string that looks like "Nmy stringP" , where N and P can be any character. How can I edit it into "my string" in C? 回答1: To "remove" the 1st character point to the second character: char mystr[] = "Nmy stringP"; char *p = mystr; p++; /* 'N' is not in `p` */ To remove the last character replace it with a '\0' p[strlen(p)-1] = 0; /* 'P' is not in `p` (and it isn't in `mystr` either) */ 回答2: Another option, again assuming that "edit" means you want to modify in place: void topntail

Why is strdup considered to be evil

淺唱寂寞╮ 提交于 2019-12-28 03:02:08
问题 I've seen some posters stating that strdup is evil. Is there a consensus on this? I've used it without any guilty feelings and can see no reason why it is worse than using malloc / memcpy . The only thing I can think might earn strdup a reputation is that callers might misuse it (eg. not realise they have to free the memory returned; try to strcat to the end of a strdup'ed string). But then malloc'ed strings are not free from the possibility of misuse either. Thanks for the replies and

Exception thrown at 0x0F640E09 (ucrtbased.dll) in ConsoleApplication5.exe: 0xC0000005: Access violation writing location 0x014C3000?

我怕爱的太早我们不能终老 提交于 2019-12-25 18:53:16
问题 I just compiled this code, and it showed me this error: Exception thrown at 0x0F640E09 (ucrtbased.dll) in ConsoleApplication5.exe: 0xC0000005: Access violation writing location 0x014C3000. I literally have no idea what this error means as I've just been using C++ for a couple months, and I've also tried looking on any other websites to look for help, but I didn't find any. For this code, I'm only allowed to use the c-string functions and the <cstring> library. I cannot use the string object

strncpy and strcat not working the way I think they would c++

扶醉桌前 提交于 2019-12-24 22:38:34
问题 I have an assignment to implement a string object ourselves, and am currently stuck when trying to concatenate two such strings. I figured I would go this route: allocate big enough space to hold insert beginning of holding string into new space up to index using strncpy(this part works) cat on the string I am inserting cat on the remainder of the holding string Implementation: #include <iostream> #include <cstring> using namespace std; int main(){ int index = 6;//insertion position char *

getting console input for Cstrings

Deadly 提交于 2019-12-24 09:14:48
问题 note: this is in C++ but using C-style strings hello SO, I'm working on an assignment and I need to get input from the console and save it to a cstring. Everything compiles fine, but when the program runs, it just skips over getting input from the user. So it will output: "Enter string to be inserted: " then skip the cin.getline function, then execute the next command. Here's my header files, the declaration of the cstring, and the line of code I'm having trouble with. #include "stdafx.h"