Format a string to specific format - c

//take the buffer, format it into a **write test.c 4:[this is what I want written in the file]**
void saveFile(Request request) {
//user inputted save test.c 4:[this is what I want written in the file]
char command[5] = "write";
for (int i = 0; i < 5; ++i) {
request[i] = command[i];
}
}
The issue is my logic will format the string into writetest.c 4:[this is what I want written in the file]
I want to add space between write and the next char

I solved the issue :)
void saveFile(char * request) {
//user inputted save test.c 4:[this is what I want written in the file]
char command[5] = "write";
char copyWithExtraBytes[1000];
strcpy_s(copyWithExtraBytes,strlen(request)+1, request);
for (int i = 0; i < strlen(copyWithExtraBytes); ++i) {
copyWithExtraBytes[i] = command[i];
if (i == 4)
{
break;
}
}
for (int i = 5; i < (strlen(copyWithExtraBytes))+1; ++i) {
if (i ==5)
{
copyWithExtraBytes[5] = ' ';
continue;
} else
{
copyWithExtraBytes[i] = request[i -1];
if (i == strlen(request))
{
copyWithExtraBytes[i+1] = request[i-1];
}
}
}
printf("%s",copyWithExtraBytes);
}

Related

why isn't this code storing the words in this 2d array?

The code is suppose to take in 6 words starting with a, b or c and put them in to each respective array. Only problem is that it doesn't store anything at all no matter what I do.
The printf in the last part of the code is suppose to spit everything that I typed in out but it's giving me nothing but blanks...
#include <stdio.h>
int main()
{
char startswithA[6][10] = {};
char startswithB[6][10] = {};
char startswithC[6][10] = {};
char holder[6][10] = {};
printf("Enter a word starting with a, b or c (write 2 of each)\n");
for (int i = 0; i < 6; i = i + 1)
{
printf("Now entering word #%d\n", i+1);
scanf_s("%s", &holder[i]);
if(holder[i][0] == 'a')
{
for(int a = 0; a < 10; a = a + 1)
{
holder[i][a] = startswithA[i][a];
}
}
else if (holder[i][0] == 'b')
{
for(int a = 0; a < 10; a = a + 1)
{
holder[i][a] = startswithB[i][a];
}
}
else if (holder[i][0] == 'c')
{
for(int a = 0; a < 10; a = a + 1)
{
holder[i][a] = startswithC[i][a];
}
}
else
{
printf("something out of bound was typed in");
}
}
for(int b = 0; b < 6; b = b + 1)
{
printf("%s %s %s\n", startswithA[b], startswithB[b], startswithC[b]);
}
return 0;
}
Looks like it is typo
holder[i][a] = startswithA[i][a];
Did you mean?
startswithA[i][a] = holder[i][a];

stdin to an emscripten, about how to continue input?

related to Providing stdin to an emscripten HTML program?
function stdin() {
if (i < input.length) {
var code = input.charCodeAt(i);
++i;
return code;
} else {
return null;
}
}
input.length should be length+2. then stdin will continue, why?
example:
function stringToBuffer(value: string): Uint8Array {
let view = new Uint8Array(value.length);
for (let i = 0, length = value.length; i < length; i++) {
view[i] = value.charCodeAt(i);
}
return view;
}
input = stringToBuffer("123");
i=0;
out();
input = stringToBuffer("xyz");
i=0;
out();
c file
int out()
{
int chr;
while ((chr = fgetc(stdin)) != EOF)
{
fputc(chr, stdout);
}
return 1;
}

Getting maze algorithm to work on open maze

first time using the site. I am trying to figure out how to solve a 'maze' using the shortest path. The code works for traditional mazes but the path I am trying to work on is essentially more open. When run the current path goes right, then down, then left and goes up then turns right before finally reaching B. My solution needs to go right then up then left to B. Any help would be appreciated!
9,11
xxxxxxxxxxx
x......B..x
x...xxxx..x
x...xxxx..x
x....A....x
x..xx.xx..x
x.........x
x.........x
xxxxxxxxxxx
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
char** maze;
int** checked;
int rows;
int cols;
int start_row;
int start_col;
enum area
{
space,
wall,
end,
trail,
};
void alloc_maze() {
maze = malloc(rows * sizeof(char*));
for (int i = 0; i < rows; i++) {
maze[i] = malloc(cols * sizeof(char*));
}
}
void alloc_checked() {
checked = malloc(rows * sizeof(char*));
for (int i = 0; i < rows; i++) {
checked[i] = malloc(cols * sizeof(char*));
}
}
void get_maze(const char* file_name)
{
char c;
char rows_t[3] = { '\0' };
char cols_t[3] = { '\0' };
int rows_i = 0;
int cols_i = 0;
int swap = 0;
FILE* file = fopen(file_name, "r");
if (file) {
while ((c = getc(file)) != EOF) {
if (c == '\n') {
break;
} else if (c ==',')
{
swap = 1;
}
else if (!swap) {
rows_t[rows_i] = c;
rows_i++;
}
else {
cols_t[cols_i] = c;
cols_i++;
}
}
}
rows = atoi(rows_t);
cols = atoi(cols_t);
alloc_maze();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
c = getc(file);
if (c == '\n') {
c = getc(file);
}
maze[i][j] = c;
if (c == 'A') {
start_row = i;
start_col = j;
}
}
}
fclose(file);
}
void get_checked() {
alloc_checked();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (maze[i][j] == 'x') {
checked[i][j] = wall;
}
else if (maze[i][j] == 'B') {
checked[i][j] = end;
}
else {
checked[i][j] = space;
}
}
}
}
int search(int row, int col) {
int* current = &checked[row][col];
if (*current == end) {
printf("\n congrats you found the shortest path is");
return 1;
}
if (*current == space) {
*current = wall;
if (search(row, col + 1)) {
*current = trail;
printf("E");
return 1;
}
if (search(row + 1, col)) {
*current = trail;
printf("N");
return 1;
}
if (search(row - 1, col)) {
*current = trail;
printf("S");
return 1;
}
if (search(row, col - 1)) {
*current = trail;
printf("W");
return 1;
}
}
return 0;
}
void add_trail() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (maze[i][j] != 'A'){
if (checked[i][j] == trail) {
maze[i][j] = 'O';
}
}
}
}
}
void print_checked() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", checked[i][j]);
}
printf("\n");
}
printf("\n");
}
void print_maze() {
printf("\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%c", maze[i][j]);
}
printf("\n");
}
printf("\n");
}
int main()
{
get_maze("quickest_route_4.txt");
get_checked();
print_maze();
search(start_row, start_col);
add_trail();
print_maze();
return 0;
}
You have implemented a DFS - Depth First Search maze searching algorithm. This means that your searching algorithm is going to explores as far as possible along a direction before trying another one.
In your code this means that it will always try all of the options going right and then all of the options that are going down and this causes your code to not find the shortest path but just finding a path.
If you do want to find the shortest path you should implement a BFS - Breadth First Search algorithm, it will find you the shortest path since it is progressing the search in all of the active nodes at the same time. It will be a bit harder to implement though since it uses a queue data structure.
Good luck
Also notice that the path you are printing is coming out in reverse order.

i am keeping seeing the dereferncing a null pointer warnning in the line "hiddenLetters[i] = '_';"?

Could someone tell me why i am keeping seeing the error dereferncing a null pointer in the line "hiddenLetters[i] = '_';"??
char* hiddenWord(char* guessWord) {
int length = strlen(guessWord);
int i;
char* hiddenLetters = NULL;
if (!(hiddenLetters = (char*)malloc(length))) {
printf("Error");
}
for (i = 0; i < length; i++) {
hiddenLetters[i] = '_';
printf("%c ", hiddenLetters[i]);
}
return hiddenLetters;
}
hiddenLetters doesn't have any elements until it passes the if check, so returning null if there was an error will suppress the warning.
char* getHiddenWord(char* guessWord) {
int length = strlen(guessWord);
char* hiddenLetters;
if (!(hiddenLetters = (char*)malloc(length))) {
printf("Error");
return NULL;
}
for (int i = 0; i < length; i++) {
hiddenLetters[i] = '_';
printf("%c ", hiddenLetters[i]);
}
return hiddenLetters;
}

program creates a file, but won't write to it

I'm very novice at C but I'm trying to learn. I'm trying right now to make a program read from a csv file and take only it's integers from it, and write those out to a seperate .txt file. I'm not seeing why it will create a file, but why it won't write anything to it. Here's the code. in the methods 'getTokensFromLineforOutput' and 'writeOutput' is where I try to do my writing, everything else works fine, and if you were to take away those two methods and run it as just a program reading from a csv file it operates as intended. But writing is where I'm running into. IDK if i'm able to attach a document so for reference here's the csv file I made from notepad:
3,1, joe
45,0, sandy
5,1, mary
12,0, eugene
11,0, alex
#include <stdio.h>
#include <stdbool.h>
#define NAMESIZE 30
#define ARRAYSIZE 100
#define ROWLENGTH 300
#define TOKENLENGTH 10
typedef struct {
int nWeeks;
bool isCensored;
char identifier[NAMESIZE];
}SurvivalPatientData;
bool giveMeALine(FILE * source, char * dest)
{
bool status = false;
char * where = dest;
char ch;
int ch_ctr = 0;
while(((ch = getc(source)) != EOF) && (ch != '\n') && (ch != '\r'))
{
*where = ch;
where++;
ch_ctr++;
status = true;
}
for (int i=ch_ctr; i<ROWLENGTH && status ;i++)
{
*where= '\0'; /*null*/
where++;
}
printf("giveMeALine returning status = %d\n",status);
return status;
}
bool getTokensFromLineforOutput(char *theLine, SurvivalPatientData *sdp) {
bool status = false;
FILE * file = fopen("C:/Users/Default/Desktop/CCSU/CCSU SPRING 18/CS_3//output.txt", "w");
char * inTheLine = theLine;
char theToken[TOKENLENGTH];
char * inToken = theToken;
char ch = '\0';
for (int i = 0; i<TOKENLENGTH; i++)
{
theToken[i]='\0'; /*null char*/
}
int value = 0;
while((ch=*inTheLine)!=',')
{
value *= 10;
value += ch - '0';
inTheLine++;
}
sdp->nWeeks=value;
fprintf(file, "%d\n",sdp->nWeeks);
inTheLine++;
for (int i = 0; i<TOKENLENGTH; i++)
{
theToken[i]='\0'; /*null char*/
}
inToken = theToken;
inTheLine++;
while((ch=*inTheLine)!=',')
{
*inToken = ch;
inToken++;
inTheLine++;
}
inToken--;
if (*inToken == '0')
{
sdp->isCensored=false;
}
else
{
sdp->isCensored= true;
}
fprintf(file, "%d\n",sdp->isCensored);
inTheLine++;
for (int i = 0; i<TOKENLENGTH; i++)
{
theToken[i]='\0'; /*null char*/
}
inToken = sdp->identifier;
while((ch=*inTheLine)!=',' && ch!= EOF)
{
*inToken = ch;
inToken++;
inTheLine++;
}
status=true;
return status;
}
bool getTokensFromLine(char * theLine,
SurvivalPatientData * sdp);
bool getTokensFromLine(char *theLine, SurvivalPatientData *sdp) {
bool status = false;
char * inTheLine = theLine;
char theToken[TOKENLENGTH];
char * inToken = theToken;
char ch = '\0';
for (int i = 0; i<TOKENLENGTH; i++)
{
theToken[i]='\0'; /*null char*/
}
int value = 0;
while((ch=*inTheLine)!=',')
{
value *= 10;
value += ch - '0';
inTheLine++;
}
sdp->nWeeks=value;
printf("Weeks is %d\n",sdp->nWeeks);
inTheLine++;
for (int i = 0; i<TOKENLENGTH; i++)
{
theToken[i]='\0'; /*null char*/
}
inToken = theToken;
inTheLine++;
while((ch=*inTheLine)!=',')
{
*inToken = ch;
inToken++;
inTheLine++;
}
inToken--;
if (*inToken == '0')
{
sdp->isCensored=false;
}
else
{
sdp->isCensored= true;
}
printf("isCensored is %d\n",sdp->isCensored);
inTheLine++;
for (int i = 0; i<TOKENLENGTH; i++)
{
theToken[i]='\0'; /*null char*/
}
inToken = sdp->identifier;
while((ch=*inTheLine)!=',' && ch!= EOF)
{
*inToken = ch;
inToken++;
inTheLine++;
}
printf("The identifier is %s\n",sdp->identifier);
status=true;
return status;
}
bool writeOutput(FILE * source, SurvivalPatientData * gotIt){
bool status = false;
bool write = false;
for (int j = 0; j<ARRAYSIZE; j++){
for (int i = 0; i<NAMESIZE; i++){
(gotIt[j].identifier)[i]='\x20';
}
gotIt[j].nWeeks = 0;
gotIt[j].isCensored = false;
}
int nrows = 0;
int row = 0;
char readALine[ROWLENGTH];
bool lines2Read = true;
bool gotTokens = false;
while (lines2Read){
bool gotALine = giveMeALine(source, readALine);
if(!gotALine){
lines2Read = false;
} else {
gotTokens = getTokensFromLineforOutput(readALine, &(gotIt[row]));
if(gotTokens){
status = true;
row++;
nrows++;
}
}
}
return status;
}
bool getInputFancy(FILE * source, SurvivalPatientData * gotIt){
bool status = false;
for (int j = 0; j<ARRAYSIZE; j++){
for (int i = 0; i<NAMESIZE; i++){
(gotIt[j].identifier)[i]='\x20';
}
gotIt[j].nWeeks = 0;
gotIt[j].isCensored = false;
}
int nrows = 0;
int row = 0;
char readALine[ROWLENGTH];
bool lines2Read = true;
bool gotTokens = false;
while (lines2Read){
bool gotALine = giveMeALine(source, readALine);
if(!gotALine){
lines2Read = false;
} else {
puts(readALine);
gotTokens = getTokensFromLine(readALine, &(gotIt[row]));
if(gotTokens){
printf("found %d row\n", row);
status = true;
row++;
nrows++;
}
}
}
printf("found %d rows\n", nrows);
return status;
}
int main() {
printf("Hello, World!\n");
bool readIt = false;
bool write = false;
SurvivalPatientData gotIt[ARRAYSIZE];
FILE * whereInputIs = fopen("C:/Users/Default/Desktop/CCSU/CCSU SPRING 18/CS_3//retake.csv", "r");
FILE * whereOutputIs = fopen("C:/Users/Default/Desktop/CCSU/CCSU SPRING 18/CS_3//output.txt", "w");
if (whereInputIs == NULL){
perror("can't open file source/doesn't exist");
}
puts("Found the file");
readIt = getInputFancy(whereInputIs, gotIt);
write = writeOutput(whereOutputIs, gotIt);
if (!readIt)
{
perror("could not read that file");
}
puts("!!!read the file!!!");
if (fclose ( whereInputIs) != 0) {
perror("trouble trying to close");
}
puts("!!!closed the file!!!");
return 0;
}
(If I could comment this I would)
Try putting parentheses around your de referencing * and variable..
(ch = (*inThLine))

Resources