Reading characters from file till white space and appending symbols

AI Thread Summary
The discussion revolves around a novice programmer seeking assistance with reading characters from a file in C++ until encountering whitespace, and appending specific symbols to the read characters. The user initially attempts to utilize the `>>` operator and `stringstream` but encounters errors related to variable naming and type mismatches. Key points include the need to include the `<sstream>` header, the case sensitivity of variable names in C++, and the incorrect assignment of a C-string to a character array. Suggestions are made to use `std::string` for easier manipulation of strings, and to avoid using a fixed-size character array due to potential buffer overruns. The conversation emphasizes the importance of proper error checking and safe coding practices, with some participants advocating for a simpler C++ solution over a C-style approach. Overall, the focus is on correcting code errors and improving programming practices for handling file input and string manipulation.
nicolegrace
Messages
5
Reaction score
0
Hi I am novice to programming and trying to read each character from file till white space [next line / space] and append symbols to the read characters.

example:

say I have string

how are you
doing sir


then I should read it into char buffer as


char buff = how#how$are#are$you#you$doing#doing$sir#sir$

Can someone help me with c++ code to it .Thank you so much
 
Technology news on Phys.org
Since there isn't a function to read until white space, it would be easier to read the entire file into a large buffer (assuming this is on a PC which probably has 1GB or more of ram), then parse the data.
 
well I just figured out that I can do this using >> operator and using string stream but i have errors in that . can someone help me



#include <iostream>
#include <string>
#include <stdio.h>
#include <fstream>


using namespace std;

int main (int argc, char **argv)
{
std::stringstream result;
std::string currentWord;
char Pat[500];
ifstream file("exm.txt");

//while (!eof)
//{

while (file >> currentWord)
{
result << currentWord << "%" << currentWord << "&";
}

//}
pat= result.str().c_str();


}
 
Nicole, I know of three errors with your program. Two of them are simple.
So, let's get the two easy ones out of the way:

1. You need

Code:
#include <sstream>

to be able to use the type std::stringstream

2. You used a capital P for the following line.

Code:
char Pat[500];

But later you used "pat" with a lower case p. C++ is case-sensitive, which means "Pat" and "pat" are two different names. For the next part, assume you fixed this by making the P lower case.


3. Okay, now comes the harder one. This line is incorrect:

Code:
pat = result.str().c_str();

The type of pat is an array type. Arrays are not valid targets for assignment.

If you want to copy a c-string into a character array, there are a number of ways to go about it. One way is for you is to use a for loop. The c-string ends with the character '\0'. This for loop should have checks not only for '\0' but also that you have not copied more than 500 characters.

Failing to check for both things may create a vulernablility by which software may be hacked. So you should learn better habits early!

I am wondering do you really have to use a character array? It is simpler to use an std::string. For example, you can copy the contents of std::strings with the operator '='.
 
Last edited:
I want to finally get a array with elements stored at each position/index like

p[0] = c
p[1]=a
...
but if i just copy stirngs it stores complete string in the index position.
can you suggest me what I should do in this case
 
@ MisterX

do you mean like this ? but I have error , saying you cannot change form char to char.
#include <iostream>
#include <string>
#include <stdio.h>
#include <fstream>
#include <sstream>using namespace std;

int main (int argc, char **argv)
{
std::stringstream result;
std::string currentWord;
const char pat[30];
ifstream file("exm.txt");

while (file >> currentWord)
{
result << currentWord << "#" << currentWord << "$";
} for(int j=0; j< 10;j++)
{
pat[j] = result.str().c_str();
}

}
 
Code:
const char pat[30];

"const" means that you may not change the elements of pat. You should remove the const.


Code:
for(int j=0; j< 10;j++)
{
pat[j] = result.str().c_str();
}

the type of pat[j] is char

the type of result.str().c_str() is const char *

It is not correct to set one equal to the other.

To get a char from a const char *, one may use the square brackets, []. For example:

Code:
std::string s("abcd");
const char * pointer;
char c;

pointer = s.c_str();
c = pointer[0];
This would set c equal to 'a', since 'a' is the first element of the c string.


Just so you know, const char * means pointer to const char

Another issue with your for loop is that it would always attempt to get 10 characters. What if the c string from "result" was less than 10 characters?
 
Of course there's no real reason that this couldn't be done using plain ol' C and "strcat". Often the simplest solution is the best.

Code:
#include<string.h>
...
  char MyBuf[500];
  char tmpBuf[50];   
  FILE *inFile
...
  while(!feof(inFile)) {
     if (fscanf(inFile,"%s",&tmpBuf) == 1) {
        strcat(MyBuf,tmpBuf);
        strcat(MyBuf,"#");
        strcat(MyBuf,tmpBuf);
        strcat(MyBuf,"$");
        }
     }
...
 
uart said:
Of course there's no real reason that this couldn't be done using plain ol' C and "strcat". Often the simplest solution is the best.
Not really the best in this case, because it assumes less than 50 characters per read and 500 characters output, with no check whether those assumptions are true.
 
  • #10
DrGreg said:
Not really the best in this case, because it assumes less than 50 characters per read and 500 characters output, with no check whether those assumptions are true.
This is nit picking in my opinion. It is merely a code snippet to give the OP some suggestions. It's not meant to be a complete solution and the 50 and 500 (previously used by the OP) are merely for the point of example. In fact the declarations of the character buffers with those example sizes was for no other reason than to indicate the type, otherwise they wouldn't have even been included in the snippet.
 
Last edited:
  • #11
uart said:
This is nit picking in my opinion. It is merely a code snippet to give the OP some suggestions. It's not meant to be a complete solution and the 50 and 500 (previously used by the OP) are merely for the point of example. In fact the declarations of the character buffers with those example sizes was for no other reason than to indicate the type, otherwise they wouldn't have even been included in the snippet.

It's not nit-picking, and the specific values of 50 and 500 are not this issue. Regardless of these sizes, there was no checking for buffer overruns in the code you posted.

Also, let's please help the OP to write her own code.

Also, your C solution is no simpler than a C++ solution. In fact I can do this task with C++ using less lines of code, and it would be safe from buffer overruns.
 

Similar threads

Replies
2
Views
768
Replies
1
Views
25K
Replies
5
Views
2K
Replies
33
Views
5K
Replies
4
Views
1K
Replies
5
Views
5K
Back
Top