Writing from file to c-string problem

Ok so I have been pulling my hair out trying to understand this but just can not seem to get this to work. I got the file to stream into the program, but I cant seem to put it into a c style string and properly organize them into a structure. If I think I am on the right track, I try to cout to see what I have and its just numbers or infinite loops. I dont even know where to begin on how to write the function. I think you try to make a pointer to find the ':' and after that somehow copy everything before the ':' to the first part in the structure. Then increase the pointer by one and search for the next':' and then copy the inbetween to the next part of the structure and so on. I just can not seem to do it or if I was doing it right. Any help would be much appreciated.

Input

The input for program 1 will be read from a file called teams.txt. The file consists of a set of records for the teams that competed in the tournament. Each record represents one team and is made up of multiple fields that are separated by colons. The fields for each team are: the school name, the team name, the score for game 1, the score for game 2, and the score for game 3. A record in the file resembles the following:

DELAWARE ST.:BLUE HENS:187:162:171

Output

The output for program 1 will be a file called results.txt, which will contain a structure for each of the teams in the teams.txt file.
Suggested Logic for main()

Create a teamInfo variable, C-Style string, input file stream, and output file stream.

Open the file teams.txt for input and verify that it opened correctly

Open the file results.txt for binary output and verify that it opened correctly

Use getline to read the first team from the input file into the C-Style string

While there are input records in the input file

Call the makeTeam function to break the C-Style string into the various fields for the structure

Write the structure to the output file using the write command

Use getline to read the next team from the input file into the C-Style string
Endwhile

Close the input and output files

main note:

A structure can be written to a file by using the write function. For example:

teamInfo aTeam;

outFile.write( (char *) &aTeam, sizeof(aTeam) );

Function to write and use
void makeTeam( char *inputLine, teamInfo &aTeam )

This function will take the information in inputLine and break it into the 5 individual fields for a team and use them to build a teamInfo structure. It takes 2 arguments: a C-Style string that contains team information and a reference to a teamInfo structure that can be used to pass back a team.

As was mentioned above, each of the fields in the C-Style string is separated by a colon. Use the strchr function to search for a colon (':') so that each field can be isolated, strcpy (or atoi or atof ) can then be used to put the isolated string into the appropriate field of the structure.

The atoi and atof functions will be used to convert a string to its numeric representation (atoi for converting to integer and atof for converting to float/double).


Here is what I have so far.

#include <iostream>
#include <iomanip>
#include <cstring>
#include <fstream>

using namespace std;

struct teamInfo
{
char schoolName[20];
char teamName[20];
int game1score;
int game2score;
int game3score;
};

void makeTeam(char *, teamInfo&);

int main()
{
teamInfo aTeam;
char inputLine[80];

ifstream inFile;
ofstream outFile;

inFile.open("teams.txt");
outFile.open("results.txt", ios::binary);

if (inFile.fail())
{
cout << "Failed to open";
exit(1);
}

if (outFile.fail())
{
cout << "Failed to write";
exit(1);
}

while(inFile.getline(inputLine, 80))
{
makeTeam(inputLine, aTeam);
}

inFile.close();
outFile.close();

system("pause");

return 0;
}

void makeTeam(char *inputLine, teamInfo &aTeam)
{

}
Is there any particular reason why you don't use stl strings? It would be much simpler.
I agree it would be much easier, but the teacher is requiring it to be done in a C style string.
so, if i get you right, your problem is extracting single lines from the fstream?

I don't find the problem there, g++ does compile it correctly.

Edit:

Try to enlarge the Buffer (more than 80 chars, probably 256 or sth.)

Maybe try it this way, to have the executed code outside of the while condition:

1
2
3
4
5
while (inFile)
{
  inFile.getline (inputLine,256);
  makeTeam (inputLine,aTeam);
}
Last edited on
Ok so I figured it out a little more. I can stream it in alright, I think im doing that right. But my main problem is being able to separate the ':' and then copying the information into the structure. Thats what I am working with. I get the schools names to come up but I can't get passed that.

while(inFile.getline(inputLine, 80))
{
makeTeam(inputLine, aTeam);
}

inFile.close();
outFile.close();

system("pause");

return 0;
}

void makeTeam(char *inputLine, teamInfo &aTeam)
{
int i = 0;
int ptr;

while(inputLine[i] != ':')
{
aTeam.schoolName[i] = inputLine[i];
i++;
}
while(inputLine[i] != ':')
{
aTeam.teamName[i] = inputLine[i];
i++;
}

cout << endl << aTeam.schoolName << endl << aTeam.teamName;

}
1. You have to increment i after the first loop.
Edit: this is because if the first loop stops, until the next one, the condition will still be false, and therefore the while loop will not even run once.
2. You need to seperate the index variable of inputLine and the other (This code writes the teamName to the same place as it is in InputLine, so if the teamName starts at pos 20, it would be written to 20 and the following in the target array)
3. Add a condition so that the prog cannot write to teamName [20] and so on (not declared). This would cause crash for names longer than 19 chars.

Hope it helps
Last edited on
Thanks for trying to help, but I just do not understand this at all. I came up with this but for some reason I can not input the team names.

void makeTeam(char *inputLine, teamInfo &aTeam)
{
     int i = 0;
     
     while(inputLine)
     {
      if(inputLine[i] == ':')
      {
         i++;
         break;
      }
      else
      {              
      aTeam.schoolName[i] = inputLine[i];
      i++;
      }
     }
     
     while(inputLine)
     {
      if(inputLine[i] == ':')
      {
          i++;
          break;
      }
      else
      {
      aTeam.teamName[i] = inputLine[i];
      i++;
      }
     }
           
     cout << endl << aTeam.schoolName << endl << aTeam.teamName;
     
}


What about this:
(not tested but should do fine)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

void makeTeam(char *inputLine, teamInfo &aTeam)
{
     unsigned int i = 0; //Current Position in inputString
     unsigned int j = 0; //CurrentPosition in team/school Name
     
      while (inputLine[i] != ':' && i<INBUFFERLEN && j<NAMELEN) //for safety
     {
          aTeam.schoolName[j] = inputLine[i]; //Put Current Char to Current Position in target
          i++;
          j++;
      }
     i++;
    j=0; //RESET position, now the team name is written to the string starting at 0!

    while (inputLine[i] != ':'&& i<INBUFFERLEN && j<NAMELEN) //dfor safety
     {
          aTeam.teamName[j] = inputLine[i];
          i++;
          j++;
      }

cout << endl << aTeam.schoolName << endl << aTeam.teamName;

//Here comes the rest
}



I edited the safety conditions

NAMELEN and INBUFFERLEN define the size of the arrays (The Buffer and the Names)
Last edited on
Wow thanks, I had to get rid the constants for safety in the while because they were giving me this error. "expected primary-expression before '=' token". Also I am curious on why it has garbage after the names is that because I took out the safety conditions?
Hm don't know what garbage you mean, but could it be that the error results from not having declared the consts? ;)

1
2
3
#define INBUFFERLEN 256
#define NAMELEN 64
//Only example Values 


But, apart from that, does it work now?
Yeah, thank you so much. What I meant by the garbage is that its printing out randon symbols from the arrays in the struct. I still need to add the null terminator. Did not realize that until just after I asked the question, and I saw that I accidentally put a equals in the declaring of constants. Thank you so much though.
Yes, you need to add a terminating '\0'...getline() might not do that for you. (You could also zero the array first).
Topic archived. No new replies allowed.