C :: Using Strtok And Strcpy In File
Sep 27, 2014How to use strtok and strcpy in a file?
View 1 RepliesHow to use strtok and strcpy in a file?
View 1 RepliesI wrote this program, it's supposed to take a user-specified file, which has a bunch of numbers, and add all the numbers in a line and give me a sum and average.
Sample file:
------------------------------------
2 1 2
3 1 2 3
4 1 2 3 4
------------------------------------
This would output:
------------------------------------
Group Size = 2
i = 1 number = 1 Sum = 1
i = 2 number = 2 Sum = 3
Average = 1.50
Group Size = 3
i = 1 number = 1 Sum = 1
i = 2 number = 2 Sum = 3
i = 3 number = 3 Sum = 6
Average = 2.00
Group Size = 4
i = 1 number = 1 Sum = 1
i = 2 number = 2 Sum = 3
i = 3 number = 3 Sum = 6
i = 4 number = 4 Sum = 10
Average = 2.50------------------------------------
// END CONTEXT
Anyway, I wrote this program. Here it is. But strtok doesn't want to advance to teh next token?
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void){
int a, sum, number, groupsize;
}
[code].....
I'm trying to read a file from the 2nd argument, skipping the first line of the file since it's the name of the columns, and separate each line as a token, by date, subject, startTime, endTime, and location.Here's my code that I got so far:
Code:
void readAppointment(){
if (arg != 2)
exit;
else {
FILE *fp;
fp = fopen( argv[1], "r");
char line[999];
[code]....
In the assignment we are forbidden to use fscanf(). I have been trying to get this to work, but I've started to realize that I do not have a complete understanding of what strtok() actually does. I'm getting this warning when debugging: "assignment makes integer from pointer without cast."
This warning happens when assigning str to goal and assist, and I think it is because they are, when dereferenced, integers. The code below correctly assigns the name into the correct spot, but leaves nonsense data in the goal and assist arrays.
ex:-7880, -7888 file example: NAME GOALS ASSISTS JOHN 1 2
Code:
void readLinesFromFile( FILE* Ptr, int* goal, int* assist, char** name, int lines ){/*
* Reads lines from files and populates the arrays with the corresponding info.
*/
int index;
char hold[ MAX_LINE ] = { 0 };
char* str = NULL;
[Code] .....
From what I understand about strtok(), it returns a string, and takes in a character array and a key value that tells it when to stop. In the online examples I've seen, they use NULL in the first field. I'm not sure why.
how to use strcpy on a 2D array. The names of the students are suppose to go along with the correct score(descending order).
Student's name are:
Jack 83
Pete 100
Shawn 75
Tim 40
Turns out according to their score:
Pete 100
Jack 83
Shawn 75
Tim 40
Code:
char students[20][50];
char name[20];
temp = 0;
for(i = 0; i < count; i++){
for(j = i + 1; j < count; j++){
if(averageScore[j] > averageScore[i]){
temp = averageScore[i];
[Code]....
I am attempting to assign "randomChoice" as the array index of the "listOfWords" array then copy "listOfWords[randomChoice] to "selectedWord". I am not sure if it is that I do not fully understand the use of strcopy() or something wrong with my arrays.
This is my error:
Code:
hangman.cc: In function ‘void SelectWord(char (*)[80], int, char*):
hangman.cc:84: error: invalid conversion from 'char' to 'const char*' And my code:
Code: #include <iostream>#include <fstream>
#include <cstdlib>
#include <cassert>
#include <cstring>
[code].....
I am trying to practice different concepts in C++ programming. Following is the program which compile successfully but gives seg fault at runtime.
#include <iostream>
#include <string.h>
using namespace std;
class A {
char *name;
int i,j;
const double k;
static float l;
int &m;
[Code] ....
I am trying to implement own strcpy function, but no output is being printed.
#include<stdio.h>
#include<string.h>
void strcpy_own(char dest[], char src[], int len);
int main() {
char src[15]="jeevan", dest[15];
[Code] ....
This for loop replaces the stars ******** in an array that contains a word to be guessed with the correct letter (c) that the user inputs if the word contains that letter.
Problem: After every guess the same hidden string of stars ******* is displayed instead of ex: ***W**** (assuming W was entered by the user)
How can I update the old ******** string in the Stars array with the new string that includes the correct letters chosen, so after every correct guess at a letter in the word a new string is displayed including the correct letters chosen?
I'm pretty sure I have to use strcpy but not sure how to implement it using a loop.
Code:
for(i = 0; i < strlen(unscrambledWord); i++) {
if(unscrambledWord [i] == c) {
Stars[i] = c;
} }
Assuming that we have the string :
Code:
***HELLO&SIR. & and * is the delimiters.
My question is how strtok will work in the beginning?
According to this to determine the beginning and the end of a token, the function first scans from the starting location for the first character not contained in delimiters (which becomes the beginning of the token). And then scans starting from this beginning of the token for the first character contained in delimiters, which becomes the end of the token. The scan also stops if the terminating null character is found.
This end of the token is automatically replaced by a null-character, and the beginning of the token is returned by the function. strtok will replace the first * with '' and the next call will start from the second * puts '' there and again the same with the third * ?
I am beginner in C++ programming. And i was try use STRTOK code with NULL fields, but in ARRAY fields NULL values is skiped. For example:
input text in Memo2:
AnsiString PAT02[100][20];
for (int IndexVRadku02 = 0; IndexVRadku02 < Memo2->Lines->Count ; IndexVRadku02++) {
AnsiString PNF02 = Memo2->Lines->Strings[IndexVRadku02];
char * PN02;
[Code] ....
Result:
Array 00 01 02 03 04 05
00 0000 TEXT1 TEXT2
01 0002 TEXT3 TEXT4 TEXT5
02 0003 TEXT6
But i need this result:
Array 00 01 02 03 04 05
00 0000 TEXT1 TEXT2
01 0002 TEXT3 TEXT4 TEXT5
02 0003 TEXT6
I'm playing around with parts of code and am coming across some errors. Most of my concern is related to strtok(). I've used it before but with a char* named token. I used a while loop to continuously check whether token was equal to NULL. In the following code, however, there aren't any checks. I was wondering if that is why this code prints (null) while running. Also, I would like to know if it is possible to read input like this code attempts to do - assigning tokens to each variable one after the other.
The format of the input:
Zucchini, Squash, pound
Yellow, Squash, pound
Tomatoes, Ugly Ripe, each
#include <stdio.h>
#include <stdlib.h>
[code]....
I am having a problem with the output of a file. My function does everything it needs to do with the exception of one line. I have a .dat file with the following:
Code:
joe clint james brint howard
jimmy
alexander
*
me
When I run the function my output is as such:
Code:
file opened
word: joe
digit: 3
word: clint
digit: 5
word: james
[Code] .....
ERROR non alphanumeric digit on line 4!
File closed "Howard" is only six digits in length. Also it seems to skips a line in the output which could be the extra character but I am not sure. What I find very interesting is the other names, (jimmy & alexander) on separate lines do not have that problem.
Here is my function:
Code:
#include "main.h"
#include "fileCheck.h"
int fileCheck(FILE *fp){
int line_count = 0;
int ret_val = 0;
int digit_count = 0;
char file[BUFF];
[Code] .....
How I can correct this? I am aware of sscanf to parse the line but I do not know the format of the file. All I know is the file will be alphanumeric and if there is a non-alphanumeric, I must print out an error with the line it occurred on such as the above output.
I am trying to make simpler equivalent of strtok().i want to separate string for my command line program. here is what i came up with
Code:
PVOID CmdArgs(PWCHAR Arg){
PWCHAR Return[10];
int I=0,s=0;
}
[code]....
i can not use winapi or standard c library because project im doing is pure ntdll api program (subsystem native in other words)
I have a string like "THIS::IS::THE:EXAMPLE::STRING"
I want to split the string to tokens based on "::".
The tokens should be:
THIS
IS
THE:EXAMPLE
EXAMPLE
STRING
I had an assignment to do a palindrome program but im stuck in this part.
"This c-string will be sent to a function that will remove all spaces and punctuation marks from its c-string as well as change any uppercase letters to lowercase. Use string tokens to eliminate the spaces & punctuation marks. Make sure you include the NULL as you concatenate the tokens into a c-string. Then copy back into the original c-string for the pass by reference."
void tokenptr(char drome[]) {
char *token;
size_t i;
token = strtok(drome, " ,.!:;?");
while (token != '