Thread: Read Int & String from a File

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    1

    Question Read Int & String from a File

    Hi,

    I'm having trouble reading an int and character string from a text file. They are separated by a single space.

    For example the file would be formatted like this:
    7 I like strings
    844 This is another
    string
    49 A
    third string

    The string can contain newlines and whitespaces and I only have to store the first 300 characters. I'm only trying to scan one line.

    I'm using the following variables.

    char buffer[300];
    char string[300];
    int length;
    FILE *fp;

    I read from previous posts on this site that fscanf will not work properly with whitespace so I'm using fgets and then scanning that string for the int and string instead.

    fgets(buffer, 300, fp);
    sscanf(buffer, "%d %s", length, string);


    The file is opened and closed properly. When I print buffer, it contains the correct data (both an integer and the string), but length and string do not. The program runs, but crashes at sscanf.

    Thanks for your help.

    Tim

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    sscanf() needs pointers as arguments. Try something like -

    sscanf(buffer, "%d %s", &length, string);

  3. #3
    Unregistered
    Guest
    with streams I would try using the peek() and/or putback(), method of istreams.


    Code:
    char ch;
    int charIndex = 0;
    int stringIndex = 0;
    char buffer[300][300];
    
    int intIndex = 0;
    int nums[300];
    
    fin >> ch;
    while(!fin.eof() && intIndex < 300 && stringIndex < 300)
    {
      if(isdigit(ch))
      {
         fin.putback(ch); 
         fin >> num[intIndex++];
          fin >> ch;
      }
      else if (ch == ' ' && charIndex < 299)
      {
        if(isdigit(fin.peek())
        { 
            buffer[stringIndex++][charIndex] = '\0';
            charIndex = 0;
        }
        else
        {
           buffer[stringIndex][charIndex++] = ch;
        }
      }
      else if (charIndex < 299)
      {
         buffer[stringIndex][charIndex++] = ch;
      }
    }
    But that looks rather silly (and doesn't deal with the issue of what to do if charIndex == 299 yet either). So if at all possible I would try to change the file structure to delimit the string from the int using a ~ (or some other char not otherwise allowed in the file) rather than a space because then I could use getline() easily.

  4. #4
    Unregistered
    Guest
    I can't edit my posts in the original post. This should correct some of the more onerous mistakes of my previous post. Needless to say I have not compiled the following to see if it even works. It's just how I would approach the problem if I couldn't change the file format.

    Code:
    char ch;
    int charIndex = 0;
    int stringIndex = 0;
    char buffer[300][300];
    
    int intIndex = 0;
    int nums[300];
    
    ifstream fin("text.txt");//use whatever file name you are to read
    
    fin >> ch;
    while(!fin.eof() && intIndex < 300 && stringIndex < 300)
    {
      if(isdigit(ch))
      {
         fin.putback(ch); 
         fin >> num[intIndex++];
         fin.ignore();
      }
      else if (ch == ' ' && charIndex < 299)
      {
        if(isdigit(fin.peek()))
        { 
            buffer[stringIndex++][charIndex] = '\0';
            charIndex = 0;
        }
        else
        {
           buffer[stringIndex][charIndex++] = ch;
        }
      }
      else if (ch != ' ' && charIndex < 299)
      {
         buffer[stringIndex][charIndex++] = ch;
      }
       fin >> ch;
    }
    but I really do suggest trying to alter the file format if you possibly can.

  5. #5
    Unregistered
    Guest
    having reread the original post to the thread (for the thrid time) it appears that the int and the string up to the next int all go in the same string, so that makes some of my code irrelevant. Nevertheless, you get an idea of what you could do with istreams and istream member fucntions to solve the problem, even if I didn't answer your question specifically, or with the desired file handling. Maybe I better just call it a night and leave it go at that.

    Good luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM