Reading characters from file till white space and appending symbols

In summary, the conversation discusses a novice programmer's attempt to read and append symbols to each character in a file until a white space or new line is reached. Various solutions are suggested, including using the >> operator and string stream, as well as using the c_str() function and a for loop. The conversation also addresses errors and potential vulnerabilities in the code. Ultimately, it is recommended to use the strcat function in plain C as a simple and efficient solution.
  • #1
nicolegrace
5
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
  • #2
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.
 
  • #3
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();


}
 
  • #4
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:
  • #5
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
 
  • #6
@ 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();
}

}
 
  • #7
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?
 
  • #8
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,"$");
        }
     }
...
 
  • #9
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.
 

1. How do I read characters from a file in a scientific format?

To read characters from a file in a scientific format, you can use the fscanf function in C or the read_csv function in Python. These functions allow you to specify the format of the data you are reading, including white space and symbols.

2. What is the purpose of reading characters till white space?

Reading characters till white space allows you to extract specific pieces of information from a larger data set, such as a text file. This can be useful for analyzing data or performing calculations on specific data points.

3. How do I append symbols to a file while reading characters?

To append symbols to a file while reading characters, you can use the fputc function in C or the write function in Python. These functions allow you to add specific characters or symbols to a file as you read through it.

4. Is reading characters from a file till white space a common practice in scientific research?

Yes, reading characters from a file till white space is a common practice in scientific research. This allows researchers to extract specific data points from large data sets, such as experimental results, and analyze them more easily.

5. Can I read characters from a file till white space using any programming language?

Yes, most programming languages have built-in functions or libraries that allow you to read characters from a file, including white space and symbols. Some common languages used in scientific research include C, Python, R, and MATLAB.

Similar threads

  • Programming and Computer Science
Replies
1
Views
349
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
4
Views
744
Replies
10
Views
959
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
Replies
1
Views
24K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
18
Views
2K
Back
Top