Reading characters from file till white space and appending symbols

Click For Summary

Discussion Overview

The discussion revolves around reading characters from a file until whitespace and appending specific symbols to the read characters using C++. Participants explore different coding approaches, error handling, and the use of C versus C++ methods.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant seeks help with reading characters from a file and appending symbols, providing an example of the desired output format.
  • Another participant suggests reading the entire file into a buffer and then parsing it, as there is no built-in function to read until whitespace.
  • A participant mentions using the extraction operator (>>) and string streams but encounters errors, prompting requests for assistance.
  • Errors in the provided code are identified, including missing includes, case sensitivity issues, and incorrect assignment of character arrays.
  • Concerns are raised about using a character array versus an std::string for storing results, with suggestions for safer coding practices.
  • One participant expresses a desire to store individual characters in an array but struggles with copying strings correctly.
  • Another participant points out that using "const" in the character array declaration prevents modification, suggesting its removal.
  • Discussion includes a C solution using "strcat" for string manipulation, with some participants debating the merits of this approach versus a C++ solution.
  • Concerns are raised about assumptions made in the C solution regarding buffer sizes and the lack of checks for buffer overruns.
  • Some participants argue that the C solution is not necessarily simpler than the C++ approach and emphasize the importance of helping the original poster write their own code.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to solve the problem, with some favoring C++ methods and others suggesting C solutions. There is no consensus on the optimal coding strategy, and multiple competing views remain throughout the discussion.

Contextual Notes

Limitations include unresolved issues with buffer sizes, error handling, and the appropriateness of using character arrays versus strings. Participants highlight the need for checks against buffer overruns, but specific implementations remain debated.

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 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
26K
  • · Replies 10 ·
Replies
10
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 33 ·
2
Replies
33
Views
6K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 5 ·
Replies
5
Views
5K
Replies
10
Views
2K