C :: Removing Trailing Character With Strtok

Sep 18, 2013

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.

View 3 Replies


ADVERTISEMENT

C++ :: How To Pad A String With Trailing Zeros Using Ostringstream

Dec 29, 2013

The code that I have does not work.

Code:

#include <iostream>
#include <iomanip>
#include <sstream>
int main() {
std::string str3 = "99W8";

[Code] ....

View 4 Replies View Related

C :: How Strtok Will Work In Beginning

Nov 28, 2013

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 * ?

View 8 Replies View Related

C :: Using Strtok And Strcpy In File

Sep 27, 2014

How to use strtok and strcpy in a file?

View 1 Replies View Related

C :: File I/O And Strtok Not Advancing

Nov 20, 2013

I 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].....

View 4 Replies View Related

C++ :: Using STRTOK With Null Fields?

Jul 23, 2013

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

View 2 Replies View Related

C/C++ :: Strtok And Null Checking?

Mar 1, 2015

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]....

View 3 Replies View Related

C :: Tokenizing String Without Strtok / Other Functions

Jul 14, 2014

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)

View 4 Replies View Related

C/C++ :: Can Use Strtok For Splitting A String With A Certain Pattern

Oct 31, 2012

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

View 1 Replies View Related

C/C++ :: Palindrome Program - Cstrings Using Strtok Function

Feb 28, 2014

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 != '') {

[Code] .....

I'm pretty sure I did that part except how can i pass this cstring back to main? We really haven't even touched much of pointers except to use strtok().

View 2 Replies View Related

C :: Reading A File In And Separating Line With Strtok Function

May 2, 2013

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]....

View 1 Replies View Related

C :: Using Strtok In Conjunction With Fgets Function To Parse File Data

Feb 17, 2013

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.

View 5 Replies View Related

C++ :: Multi-character Character Constant Error Message?

Sep 14, 2014

I keep getting this warning message and I do not know how to fix it. Is it because I'm using char to instead of strings to replace all 't' with 'lp'?

#include<iostream>
#include<string>
#include <stdio.h>
using namespace std;
char * scanf(char * a) {

[code]....

View 6 Replies View Related

C++ :: Reading Data Character By Character From Text File

Jul 25, 2012

Double values are stored in text file. 23.5 36.8 34.2 ... My teacher told me to read them character by character and then make words, like i have to read "2" "3" "." "5" and now have to make it or treat it as word and then using atoi(). I have to convert it into double. but i dont know how to do this....

View 5 Replies View Related

C++ :: Removing Element From Map?

Jun 20, 2013

I have a map as below. MoTopImpl is a class.

Map<MoTopImpl*,os_Reference_protected<MoTopImpl> > map_;

The map is populated as below:

void setMoInMap(MoTopImpl* mo,MoTopImpl* me) {
map_[mo] = me;
}

Now, I want to remove a specific element from this map. I am passing the element to be removed to the remove function as below:

void Asap::removeMoFromMap(MoTopImpl* mo) {
// First solution
if (mo != 0) {

[Code]....

And the function removeMoFromMap is called as below:

MoTopImpl* moTop = getMoTopImpl();
if (moTop != 0)
removeMoFromListMosSuspended(moTop);

But I am able to empty the map by iterating through the complete map as below:

Mapiter<MoTopImpl*,os_Reference_protected<MoTopImpl> > moIter(map_);
for (moIter = map_.first(); moIter; moIter.next()) {
moIter.remove();
}
cout << "Map zise = " << map_.size() << endl; // Prints zero

View 6 Replies View Related

C/C++ :: Removing A Name From A Vector

Nov 3, 2014

I'm supposed to create a program that stores names and then the final function is supposed to remove a name. But it only removes the first name instead of the inputted name.

the problem with names.erase(names.begin()+i)?

#include <iostream>
#include <string>//include for string function
#include <vector>//include for vectors

[Code].....

View 9 Replies View Related

C :: Read From Stdin (File) Character By Character

Nov 10, 2013

I have to optimize a code for below scenario. I am reading stdin (a file redirected to stdin) character by character. How many chars are going to come is not known. After every few chars there is a seaparator. e.g $ as below

rhhrkkj$hghjhdf$ddfkrjt

While reading, if the separator arrives I'm processing the string stored before that separator and then continue reading stdin in same fashion, till EOF. I am using getc(stdin) to read chars.

Using gprof I can see most of the program time is spent inside main() , for this reading logic. Rest of the program is just some insert and search operations. I am getting time of 0.01 secs at the moment, want to reduce further.

View 6 Replies View Related

C/C++ :: Read Input File Character By Character?

Aug 10, 2012

How do I write an a program that will read an input file character by character?

View 1 Replies View Related

C :: Removing Item From A Queue

Aug 17, 2014

I'm having a problem with removing an item from a queue. At first in the debugger I got SIGTRAP but I don't get it anymore but the problem still exists. When you try to remove an item from the queue the first nothing happens. Here's the code below compile it and you see what I'm talking about.

Code:

#include <stdio.h>
#include <stdlib.h>
struct Node {
char let;
struct Node *nextNode;
};

[code]....

View 12 Replies View Related

C++ :: Removing Characters From A String

Nov 1, 2013

I want to remove a particular character from a string. Say '.'. I've tried the following:

void removeDots(std::string &x)
{
std::remove_if(x.begin(),x.end(),[](char c){return (c == '.');});
}

This has the effect of removing the dots but keeping the length of the string the same, so old characters are left over at the end. Is there an extra action to do to x to make it the right size or is there a better way of doing it?

View 3 Replies View Related

C++ :: Removing Punctuations Off From A String

Jun 5, 2013

I'm working on a problem in which I've to design a program in which the punctuations should be removed from the string. For eg., if input str is: "Hello!!"; the output must be: "Hello".

I'm not sure how to remove a sub-string (if that's the right word!!) from a string. So, I designed a program which print out the punctuations. For eg., if input str is: "Hey!!"; the output would be: ! !

Here it is:

#include <iostream>
#include <string>
using namespace std;
int main (){
cout << "Enter a string" << endl;

[Code] ....

So, I want to know what should be added to this program so that the punctuations can be removed; or should I rewrite another program for that?

View 1 Replies View Related

C++ :: Removing All Non-doubles From A String

May 19, 2014

I have many random strings that look something like this:

" 55.343 Char 1.3825 asdf 0.1853 500 1.1359 4.0000 1 100 4.5043"

Notices how there are ints and chars and doubles in the string.

How do I remove all non-doubles for a string like this? The chars and ints may be anywhere within the string.

View 6 Replies View Related

C++ :: Removing From A Vector Of Sets?

Apr 18, 2014

I have a vector of sets in which I wish to remove a set from the vector, if it contains a certain value, is there any way of doing this?

for(int j = 0; j <= clauseVector.size(); ++j){
if(clauseVector[j].find(find) != clauseVector[j].end())
std::cout << "FOUND: " << propagator << "
";
}
}

This is what I have been using to find the element, but is there a way to remove the set that contains the element?

If needed I can include the full code

View 11 Replies View Related

C/C++ :: File I/O And Removing Duplicates

Nov 18, 2014

I have a program that's supposed to read in a file with comma seperated values. This file contains duplicates. The goal is to write a new file that does not contain any of the duplicates. I've successfully written the code to read in a file and create a new, identical file, but I'm failing at deleting the duplicates.

The format of each line in the file is: index,first_name,last_name,address,city,state,zip_code

Example: 1,John,Savage,579 Lone Street,Providence,RI,02903

As a requirement for the assignment, I've defined a Person class:

//File: Person.h
struct Person {
string index;
string first_name;
string last_name;

[Code] .....

This code writes the file I want (overlooking the duplicates) if I implement my equality operator as follows:

bool operator ==(const Person &a, const Person &B)/>
{
return false; //placeholder
}

Obviously this doesn't get the job done, since it will never detect a duplicate. The problem is that whenever I try to write any meaningful code, the program writes an empty file. The idea I've been trying to implement is to compare each of the members of Person like this:

bool operator ==(const Person &a, const Person &B)/>
{
//if two Person objects have equivalent names, they are duplicates
return ( (a.first_name == b.first_name) && (a.last_name == b.last_name) )
}

At first I thought the program was working just as before, but then deleting each line of the file as the result of an error in my code. However, I tried troubleshooting the problem by adding in

cout << a.last_name;

to parts of my code so I could see the value in certain places. Whenever I add this line or try to access a member of Person, the program writes a blank file.

View 3 Replies View Related

C# :: Removing Items From ListBox

Mar 5, 2014

I'm working on creating a windows form with a listbox, textbox, and 2 buttons (add,remove). I need a way of removing every string matching the contents of the textbox from the listbox. Here's what I have:

for (int i=0;i<listBox1.Items.Count;i++)
{
//...
listBox1.RemoveAt(i--)
}

Seems to work, but I need a way to show a error message once the user clicks 'remove' and no items in the listbox match.

View 6 Replies View Related

C/C++ :: Removing Duplicate Values From Map?

Mar 1, 2015

im working on a homework assignment t that should print all pairs of integers that sum to val. I have so far finished except It prints duplicate values (ie (3,1) and(1,3) for values that add to 4) . how can i remove these duplicate pairs of values?

#include <iostream>
#include <map>
#include <vector>
using namespace std;
void printPairs( vector<int> numbers, int val){
int i;

[code]....

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved