void initTreeData(char *file, Topo *top) { char *linebuff = NULL; size_t length = 0; ssize_t linesize; printf("[TREE] Topology detected.\n"); FILE *data = fopen(file,"r"); if(!data){ printf("Error: Something wrong occurred!"); return; } /* First three lines are NOT Link Data*/ int index; for(index = 0; index < 3; index++) { linesize = getline(&linebuff, &length, data); removeNewLine(linebuff); if(linesize != -1){ int val = atoi(linebuff); switch(index){ case 0: break; case 1: top->numhosts = val; printf("Number of hosts: %d \n", val); break; case 2: top->numswitch = val; printf("Number of switches: %d \n", val); break; } } else { printf("Something went wrong reading first three lines \n"); return; } } int count = 0; while((linesize = getline(&linebuff, &length, data)) != -1){ removeNewLine(linebuff); int num = atoi(linebuff); top->link[count] = num; count++; // Increment Counter } int num_link = count / 3; top->numlinks = num_link; printf("Number of links: %d \n", num_link); fclose(data); }
char *readLineFrom(FILE *file) { char str[1024]; fgets(str, sizeof(str), file); removeNewLine(str); return str; }
CBaseExtractor * LoadExtractor(const string & strPath, CTemplateManager & tmpMgr) { fprintf(stderr, "\nLoading from %s\n", strPath.c_str()); FILE *fp = fopen(strPath.c_str(), "r"); if (fp == NULL) { fprintf(stderr, "Loading %s failed\n", strPath.c_str()); exit(0); } char buf[1024]; fgets(buf, 1024, fp); removeNewLine(buf); CBaseExtractor *pExtor = NULL; if (string(buf) == CEnFeatureExtractor().GetName()) pExtor = new CEnFeatureExtractor(); else if (string (buf) == CChFeatureExtractor().GetName()) pExtor = new CChFeatureExtractor(); else { fprintf(stderr, "Error: unknown extractor type %s\n", buf); exit(0); } pExtor->SetTempMgr(&tmpMgr); pExtor->Load(fp); fclose(fp); return pExtor; }
int main() { char companyName[BUFFER_SIZE]; printf("Company name:"); fgets(companyName, BUFFER_SIZE, stdin); removeNewLine(companyName); char companyAddress[BUFFER_SIZE]; printf("Company address:"); fgets(companyAddress, BUFFER_SIZE, stdin); removeNewLine(companyAddress); char companyPhone[BUFFER_SIZE]; printf("Phone number:"); fgets(companyPhone, BUFFER_SIZE, stdin); removeNewLine(companyPhone); char faxNumber[BUFFER_SIZE]; printf("Fax number:"); fgets(faxNumber, BUFFER_SIZE, stdin); removeNewLine(faxNumber); char webSite[BUFFER_SIZE]; printf("Web site:"); fgets(webSite, BUFFER_SIZE, stdin); removeNewLine(webSite); char managerFirstName[BUFFER_SIZE]; printf("Manager first name:"); fgets(managerFirstName, BUFFER_SIZE, stdin); removeNewLine(managerFirstName); char managerLastName[BUFFER_SIZE]; printf("Manager last name:"); fgets(managerLastName, BUFFER_SIZE, stdin); removeNewLine(managerLastName); int managerAge; char ageInput[4]; printf("Manager age:"); fgets(ageInput, sizeof(ageInput), stdin); sscanf(ageInput, "%d", &managerAge); char managerPhone[BUFFER_SIZE]; printf("Manager phone:"); fgets(managerPhone, BUFFER_SIZE, stdin); removeNewLine(managerPhone); printf("%s\n", companyName); printf("%s\n", companyAddress); printf("Tel. %s\n", companyPhone); printf("Fax: %s\n", (faxNumber[0] != '\0' ? faxNumber : "(no fax)")); printf("Web site: %s\n", webSite); printf("Manager: %s %s (age: %d, tel. %s)", managerFirstName, managerLastName, managerAge, managerPhone); return 0; }
/**************************************************************************** REMARKS: Gets a string with the new line removed ****************************************************************************/ static void safeGetLine( GC_devCtx *gc, char *s, int len) { GetString(gc,s,len); removeNewLine(s); }
void setup_user_account(char **name){ size_t len = 0; char *buffer=NULL; printf("Enter your name: \n"); getline(&buffer, &len,stdin); removeNewLine(buffer); //*name = malloc(strlen(buffer)); //snprintf(*name,strlen(buffer),"%s",buffer); *name = buffer; // free(buffer); }
vector<_SEN *> CReader:: ReadSentences(const string &strPath, CPool &rPool, bool updateMap) { FILE * fpIn = fopen(strPath.c_str(), "r"); if (fpIn == NULL) { fprintf(stderr, "%s open failed\n", strPath.c_str()); exit(0); } static char buf[BUF_LEN]; vector<wstring> lines; vector<_SEN*> senVec; int nLine = 0; while (fgets(buf, BUF_LEN, fpIn) != NULL) { if (++nLine % 100000 == 0) fprintf(stderr, "Reading %d lines from %s....\r", nLine, strPath.c_str()); removeNewLine(buf); if (strlen(buf) == 0) { if (lines.size() != 0) { _SEN *pSen = SSentence::CreateSentence(rPool, lines, updateMap); senVec.push_back(pSen); lines.clear(); } } else { wstring line = CCodeConversion::UTF8ToUnicode(buf); lines.push_back(line); } } // handle the last one if (lines.size() != 0) { _SEN *pSen = _SEN::CreateSentence(rPool, lines, updateMap); senVec.push_back(pSen); lines.clear(); } fprintf(stderr, "%-7d sen read from %s\n", (int)senVec.size(), strPath.c_str()); fclose(fpIn); return senVec; }
int main() { char hexadecimal[BUFER]; int lineLenght; long long decimal; printf("Enter hexadecimal integer number: "); fgets(hexadecimal, BUFER, stdin); lineLenght = removeNewLine(hexadecimal); decimal = hexToDecimal(hexadecimal); printf("Decimal = %lld", decimal); return (EXIT_SUCCESS); }
/*************** ** runCommand ** **************** ** Run the system command through a pipe ** The pointer result must point to a pre-allocated array of at least BUF_SIZ */ static void runCommand (const char *command, char *result) { FILE * pipe; pipe = popen(command, "r"); if(pipe == NULL) { /* command failed */ result[0] = '\0'; } else { if(NULL == fgets(result, BUF_SIZ, pipe)){ /* command failed */ result[0] = '\0'; } pclose(pipe); } removeNewLine(result); }
vector<SSentence *> CReader:: ReadSentences(const char *pszPath, CPool &rPool) { FILE * fpIn = fopen(pszPath, "r"); CHK_OPEN_FAILED(fpIn, pszPath); char buf[BUF_LEN]; vector<wstring> lines; vector<SSentence *> vSen; vector<int> idxVec; int nLine = 0; while (fgets(buf, BUF_LEN, fpIn) != NULL) { if (++nLine % 100000 == 0) fprintf(stderr, "Reading %d lines\r", nLine); removeNewLine(buf); if (strlen(buf) == 0) { if (lines.size() != 0) { SSentence *pSen = SSentence::CreateSentence(rPool, lines, true, &idxVec); lines.clear(); vSen.push_back(pSen); } } else { wstring line = utf82wstr(buf);// CCodeConversion::UTF8ToUnicode(buf); lines.push_back(line); } } // handle the last one if (lines.size() != 0) { SSentence *pSen = SSentence::CreateSentence(rPool, lines, true, &idxVec); lines.clear(); vSen.push_back(pSen); } fprintf(stderr, "Total %lu sentences readed\n", vSen.size()); fclose(fpIn); return vSen; }
int main(int argc, char const *argv[]) { FILE *fp = NULL; char *buffer = malloc(sizeof(char) * 150); char roomElements[300] = {0}; char *roomSize = NULL; int i = 1; int *size = NULL; // does this need to be malloced? char input = '\0'; check(argc == 2, "Please enter two arguments"); fp = openFile((char*)argv[1], "r"); initscr(); noecho(); /*Draws rooms and items*/ while (fgets(buffer, 150, fp)) { removeNewLine(buffer); strcpy(roomElements, strchr(buffer, ' ')); roomSize = strtok(buffer, " "); // get the room size size = getRoomSize(roomSize, i); // convert room size to int pointer drawRoom(size, i); // draw the room tokenizeRoom(roomElements, i); // draw the elements i++; free(size); } /*This is the user input loop*/ do { input = getch(); getInput(input); } while (input != 'q'); free(buffer); fclose(fp); endwin(); return 0; error: return -1; }
int main(int argc, char **argv) { char *buffer; size_t bufsize = 32; size_t characters; buffer = (char *)malloc(bufsize * sizeof(char)); printf("To get started, whos score do you want to beat?\n"); getline(&buffer, &bufsize, stdin); removeNewLine(buffer, bufsize); printf("Ok! Time to beat %s!\n", buffer); ///////////////////////////////////// Board board = {0}; Board nextBoard = {0}; while (true) { printBoard(&board); prettyPrintPieces(PIECES); printf("Enter 3 piece numbers:\n"); int pieces[3]; scanf("%d %d %d", &pieces[0], &pieces[1], &pieces[2]); Turn turns[20]; int nTurns = getPermutations(&board, pieces, turns); int t = getMaxTurnIndex(turns, nTurns); printf(">>>>>>>\n"); printTurn(&turns[t]); executeMove(&board, &nextBoard, &(turns[t].moves[0])); executeMove(&nextBoard, &board, &(turns[t].moves[1])); executeMove(&board, &nextBoard, &(turns[t].moves[2])); cleanRows(&nextBoard, &board); } }
void update_que(char *name, mqd_t *message_que){ char *str = NULL; char *message = NULL; size_t len = 0; getline(&str, &len, stdin); message = malloc(len*sizeof(char)+9*sizeof(char)); strcat(message,"bb"); strcat(message, "."); //strcat(message,&name); //strcat(message,": "); removeNewLine(str); strcat(message, str); strcat(message,"."); strcat(message,"bb"); //strcat(message,str); //printf("\n this is the Q: %s\n", message_que); //printf("%s\n",message); send_message(message_que,message); free(message); }
int initializeTop(Topo *topology) { char filename[FILENAMEMAX]; char *linebuff = NULL; size_t length = 0; ssize_t linesize; printf("Enter file name for topology: "); scanf("%s", &filename); FILE *file = fopen(filename,"r"); if(!file){ printf("ERROR: File not found..\n"); return FAIL; } else { linesize = getline(&linebuff, &length, file); removeNewLine(linebuff); if(linesize == -1) { printf("Incorrect syntax detected..stopping.\n"); fclose(file); return FAIL; } else { if(strcmp(linebuff, tree_cmp) == 0) { //Remember to close file fclose(file); initTreeData(filename, topology); return SUCCESS; } else if (strcmp(linebuff, star_cmp) == 0) { fclose(file); initStarData(filename, topology); return SUCCESS; } else { printf("Incorrect syntax detected..stopping.\n"); fclose(file); return FAIL; } } } }
int main(){ FILE *fp = NULL; char lineIn[MAX] = ""; fp = fopen("fileInput.txt", "r"); if (!fp){ perror("Can't open fileInput.txt"); exit (EXIT_FAILURE); } printf("read these characters:\n"); while(fgets(lineIn,MAX,fp)){ removeNewLine(lineIn); printf(lineIn); } if (fclose(fp)){ perror("Can't close fileInput.txt"); exit (EXIT_FAILURE); } else { printf("\n"); exit (EXIT_SUCCESS); } }
int main (void) { char * * theArray; int i; int num; char buffer[50]; printf("How many elements in the array? "); scanf("%d", &num); getchar(); theArray = createArray(num); for (i=0; i<num; i++) { printf("enter element %d ", i); fgets(buffer, 100, stdin); removeNewLine(buffer); addElement(theArray, buffer, i); } /*printArray(theArray, num); freeArrayElements(theArray, num);*/ free(theArray); return (0); }
/** * Main function * */ int main(int argc, char* argv[]) { int i, j, cmdargv_len; int rc = 0; int pipeExists = 0; char *formattedInput = NULL; char *userInput = NULL; char **cmdargv = NULL ; char *cdCmd = NULL; char *pipeCmd = NULL; if((userInput = malloc(sizeof(char)*MAX_CMD_LEN)) == NULL) ERROR("Failed malloc\n"); cmdargv_len = sizeof(char*) * MAX_CMD_ARGS; if((cmdargv = (char**) malloc(cmdargv_len)) == NULL) { ERROR("Failed malloc\n"); } else { memset(cmdargv, '\0', sizeof(char*) * MAX_CMD_ARGS); } registerSignalHandler(); while(1) { printf("_dash > "); got_sigint = false; if (fgets(userInput, MAX_CMD_LEN, stdin) == NULL) { if (got_sigint) { printf("\n"); continue; } else { // Ctrl+D return 0; } } // TODO: Sanitize user input! We're currently hoping the user is a // benelovent, sweet human being. HA! if( (formattedInput = malloc(sizeof(userInput))) == NULL ) ERROR("Failed malloc\n"); removeNewLine(userInput, formattedInput); // See if user wants out. if((strcmp("quit", formattedInput) == 0) || (strcmp("exit", formattedInput) == 0) || (strcmp("q", formattedInput) == 0)) { printf("Quitting!\n"); goto cleanup; } // Check to see if user wants to change working directories if( (cdCmd = strstr(formattedInput, "cd ")) != NULL ) { if(changeDir(cdCmd) != 0) ERROR("cd failed."); free(cdCmd); // No need to fork/exec, can just move on. continue; } // Check to see if user wants to pipe commands if( (pipeCmd = strstr(formattedInput, "|")) != NULL ) { pipeExists = 1; // Don't need to free pipeCmd bc freeing formattedInput will take // care of that for us. //free(pipeCmd); } parseUserInput(formattedInput, cmdargv); for(j=0; cmdargv[j] != NULL; j++) printf("%d: cmdargv[%d]: %s\n", __LINE__, j, cmdargv[j]); rc = execute_fork(cmdargv, pipeExists); ASSERT( rc != 0 ); pipeExists = 0; } /* Cleanup! */ cleanup: free(formattedInput); free(userInput); if (cmdargv) { for (i = 0; i < MAX_CMD_ARGS; i++) { free(cmdargv[i]); /* free(NULL) is ok with glibc */ } } free(cmdargv); printf("All finished.\n"); return 0; }
void testApp::urlResponse(ofHttpResponse &httpResponse){ stringstream ss ; ss << "urlResponse status code : " << httpResponse.status << endl ; if ( httpResponse.status == 200 ) { ss << "name : " << httpResponse.request.name << endl ; //ss << "url : " << httpResponse.request.url << endl ; if ( httpResponse.request.name == "LIGHTS_INFO" ) { bool bResult = json.parse( httpResponse.data.getText() ) ; if ( bResult == true ) { if ( lightbulbs.size() > 0 ) { lightbulbs.clear() ; } for ( int j = 0 ; j < json.size() ; j++ ) { //cout << "opening lightbulb data : " << j << endl ; stringstream parseInput ; parseInput << json[ofToString(j)]["name"] << endl ; string _name = parseInput.str() ; removeNewLine( &_name ) ; //cout << " name : '" << parseInput.str() << "'" << endl ; //The first value is NULL so we should check for that if ( parseInput.str().find( "null" ) == -1 ) { lightbulbs.push_back( ( ofPtr< ofxHueLightbulb > ) new ofxHueLightbulb() ) ; lightbulbs[ (lightbulbs.size() -1 ) ]->setup( parseInput.str() ) ; //cout << " index : " << j << endl ; string testUrl = ipAddress + "/api/" + username + "/lights/" + ofToString( j ) ; callHueApi( testUrl , "LIGHTBULB_" + ofToString( j ) ) ; } } } } int findIndex = httpResponse.request.name.find("LIGHTBULB_" ) ; ss << "FIND INDEX : " << findIndex << endl ; if ( findIndex > -1 ) { int index = ofToInt( ofToString( httpResponse.request.name[10] ) ) - 1 ; ss << "index : " << index << endl ; cout << "Lightbulb : " << index << " data has loaded ! " << endl ; ofxJSONElement lightData ; bool bResult = lightData.parse( httpResponse.data.getText() ) ; if ( bResult == true ) { //Hue stringstream hueSs ; hueSs << lightData["state"]["hue"] ; string _hue = hueSs.str() ; //hueSs.str().substr( 0 , (hueSs.str().size()-1)) ; removeNewLine( &_hue ) ; //Saturation stringstream saturationSs ; saturationSs << lightData["state"]["sat"] ; string _saturation = saturationSs.str() ; removeNewLine( &_saturation ) ; //Brightness stringstream brightnessSs ; brightnessSs << lightData["state"]["bri"] ; string _brightness = brightnessSs.str() ; removeNewLine( &_brightness ) ; //On / Off stringstream onSs ; onSs << lightData["state"]["on"] ; string _on = onSs.str().substr( 0 , (onSs.str().size()-1)) ; //cout << "_on" << "before remove line: " << _on << endl ; removeNewLine( &_on ) ; ss << " on : '" << _on << "'" << endl ; //cout << "_hue : '"<<_hue<<"'"<<endl ; if ( index < lightbulbs.size() ) { lightbulbs[ index ]->hue = ofToFloat( _hue) ; lightbulbs[ index ]->saturation = ofToFloat( _saturation ) ; lightbulbs[ index ]->brightness = ofToFloat( _brightness ) ; lightbulbs[ index ]->bOnState = ofToBool( _on ) ; /* if ( _on.compare( "true" ) == 0 ) { cout << " ON ! : " << endl ; lightbulbs[ index ]->bOnState = true ; } else if ( _on.compare( "false" ) == 0 ) lightbulbs[ index ]->bOnState = false ;*/ } } /* {"state": {"on":true,"bri":225,"hue":45489,"sat":253,"xy":[0.1859,0.0771],"ct":500,"alert":"none","effect":"none","colormode":"xy","reachable":true}, "type": "Extended color light", "name": "Hue Lamp 5", "modelid": "LCT001", "swversion": "65003148", "pointsymbol": { "1":"none", "2":"none", "3":"none", "4":"none", "5":"none", "6":"none", "7":"none", "8":"none" }} */ } } //ofLog( OF_LOG_WARNING , httpResponse.data.getText() ) ; lastHttpResponse = httpResponse ; ofLog( OF_LOG_WARNING , ss.str() ) ; }
vector<CDepTree *> CReader:: ReadTrees(const char *pszPath, CPool &rPool, vector<SSentence *> *pSenVec, bool AddDLabel) { FILE * fpIn = fopen(pszPath, "r"); CHK_OPEN_FAILED(fpIn, pszPath); char buf[BUF_LEN]; vector<wstring> lines; vector<CDepTree *> treeVec; vector<int> idxVec; int nLine = 0; if (pSenVec != NULL) pSenVec->clear(); int nRm = 0; while (fgets(buf, BUF_LEN, fpIn) != NULL) { if (++nLine % 100000 == 0) fprintf(stderr, "Reading %d lines\r", nLine); removeNewLine(buf); if (strlen(buf) == 0) { if (lines.size() != 0) { SSentence *pSen = SSentence::CreateSentence(rPool, lines, true, &idxVec); bool puncHead = pSen->AppendPuncs(rPool, idxVec);// == true) nRm += puncHead; if (AddDLabel) for (int i = 0; i < pSen->Length(); ++i) CIDMap::AddDependencyLabel(pSen->Label(i)); CDepTree *pTree = CDepTree::BuildTree(idxVec, pSen, rPool); if (puncHead == true) { pTree->DisplayTreeStructure(stdout); fprintf(stdout, "\n"); } else treeVec.push_back(pTree); lines.clear(); if (puncHead == false && pSenVec != NULL) pSenVec->push_back(pSen); } } else { wstring line = utf82wstr(buf);// CCodeConversion::UTF8ToUnicode(buf); lines.push_back(line); } } // handle the last one if (lines.size() != 0) { SSentence *pSen = SSentence::CreateSentence(rPool, lines, true, &idxVec); bool puncHead = pSen->AppendPuncs(rPool, idxVec);// == true) nRm += puncHead; if (AddDLabel) for (int i = 0; i < pSen->Length(); ++i) CIDMap::AddDependencyLabel(pSen->Label(i)); CDepTree *pTree = CDepTree::BuildTree(idxVec, pSen, rPool); if (puncHead == true) pTree->DisplayTreeStructure(stdout); else treeVec.push_back(pTree); lines.clear(); if (puncHead == false && pSenVec != NULL) pSenVec->push_back(pSen); } fprintf(stderr, "Total %lu sentences readed, %d punc head \n", pSenVec->size(), nRm); fclose(fpIn); CIDMap::BuildOutcomeIds(); return treeVec; }
/******************** ** readProcCpuInfo ** ********************* ** Reads and parses /proc/cpuinfo on a Linux system ** The pointers must point to pre-allocated arrays of at least BUF_SIZ */ static void readProcCpuInfo (char *model, char *cache) { FILE * info; char * cp; int cpus = 0; char * buffer_end; char buffer[BUF_SIZ]; char vendor_id[BUF_SIZ]; char model_name[BUF_SIZ]; char cpu_MHz[BUF_SIZ]; int i; float f; vendor_id[0] = model_name[0] = cpu_MHz[0] = model[0] = cache[0] = '\0'; info = fopen("/proc/cpuinfo", "r"); if(info != NULL) { /* command did not fail */ while(NULL != fgets(buffer, BUF_SIZ, info)){ buffer_end = buffer + strlen(buffer); cp = buffer; if(! strncmp(buffer, "processor", 9)) { cpus++; } else if(! strncmp(buffer, "vendor_id", 9)) { cp+=strlen("vendor_id"); while(cp < buffer_end && ( *cp == ' ' || *cp == ':'|| *cp == '\t')) cp++; if(cp<buffer_end) { strcpy(vendor_id, cp); } removeNewLine(vendor_id); } else if(! strncmp(buffer, "model name", 10)) { cp+=strlen("model name"); while(cp < buffer_end && ( *cp == ' ' || *cp == ':'|| *cp == '\t')) cp++; if(cp<buffer_end) { strcpy(model_name, cp); } removeNewLine(model_name); } else if(! strncmp(buffer, "cpu MHz", 7)) { cp+=strlen("cpu MHz"); while(cp < buffer_end && ( *cp == ' ' || *cp == ':'|| *cp == '\t')) cp++; if(cp<buffer_end) { strcpy(cpu_MHz, cp); } removeNewLine(cpu_MHz); } else if(! strncmp(buffer, "cache size", 10)) { cp+=strlen("cache size"); while(cp < buffer_end && ( *cp == ' ' || *cp == ':'|| *cp == '\t')) cp++; if(cp<buffer_end) { strcpy(cache, cp); } removeNewLine(cache); } } if(cpus>1) { if (cpus==2) { strcpy(model, "Dual"); } else { sprintf(model, "%d CPU", cpus); } } cp = model + strlen(model); if(vendor_id[0] != '\0'){ if(cp != model){ *cp++ = ' '; } strcpy(cp, vendor_id); cp += strlen(vendor_id); } if(model_name[0] != '\0'){ if(cp != model){ *cp++ = ' '; } strcpy(cp, model_name); cp += strlen(model_name); } if(cpu_MHz[0] != '\0'){ if(cp != model){ *cp++ = ' '; } f = atof(cpu_MHz); i = (int)(f+0.5f); sprintf(cpu_MHz, "%dMHz", i); strcpy(cp, cpu_MHz); cp += strlen(cpu_MHz); } fclose(info); } }