Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Reading characters from file till white space and appending symbols

  1. Sep 23, 2011 #1
    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.


    say I have string

    how are you
    doing sir

    then I should read it in to 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
  2. jcsd
  3. Sep 23, 2011 #2


    User Avatar
    Homework Helper

    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.
  4. Sep 23, 2011 #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();

  5. Sep 24, 2011 #4
    Nicole, I know of three errors with your program. Two of them are simple.
    So, lets get the two easy ones out of the way:

    1. You need

    Code (Text):
    #include <sstream>
    to be able to use the type std::stringstream

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

    Code (Text):
    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 (Text):
    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: Sep 24, 2011
  6. Sep 24, 2011 #5
    I want to finally get a array with elements stored at each position/index like

    p[0] = c
    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
  7. Sep 24, 2011 #6
    @ MisterX

    do you mean like this ? but I have error , saying you cannot change form char to char.

  8. Sep 24, 2011 #7
    Code (Text):
    const char pat[30];
    "const" means that you may not change the elements of pat. You should remove the const.

    Code (Text):
    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 (Text):

    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?
  9. Sep 25, 2011 #8


    User Avatar
    Science Advisor

    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 (Text):

      char MyBuf[500];
      char tmpBuf[50];  
      FILE *inFile
      while(!feof(inFile)) {
         if (fscanf(inFile,"%s",&tmpBuf) == 1) {
  10. Sep 25, 2011 #9


    User Avatar
    Science Advisor
    Gold Member

    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.
  11. Sep 25, 2011 #10


    User Avatar
    Science Advisor

    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: Sep 25, 2011
  12. Sep 25, 2011 #11
    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook