Reputation: 317
Good evening!
I'm finishing up my first semester of programming in this C++ class (new to programming altogether) and have a final project. What we're asked to do is create a mock social media network (similar to Facebook) where you can sign up, make posts, follow other users, and other such basic functions.
The basis idea behind the project is to write and append new info to a text file and use that as a database.
One of the problems I'm running into right now is that I found out that it's only possible to read a specific line of a file if all the lines are of the same length. The issue arises when, for example, I want to view a user's public information such as their name and occupation is that I would have to read from a specific line, but the lines may contain different lengths of information. For example, if different users follow different numbers of people, that would make it so that not every line has the same number of words.
Here's a very condensed skeleton code of my project so far for the sake of saving some space:
project.cpp (implementation)
#include <iostream>
#include <string>
#include "project02.h"
using namespace std;
void Login()
{
//login screen, if user chooses to sign up then it calls setUserInfo()
}
//includes setter and getter functions but I won't need to list them here
project.h (header)
#ifndef PROJECT02_H
#define PROJECT02_H
using namespace std;
void Login();
class UserInfo
{
//class for setting and getting user information
};
projectmain.cpp (main)
#include <iostream>
#include <string>
#include "project02.h"
using namespace std;
int main()
{
Login();
return 0;
}
That's the gist of it. When a new user signs up all of the inputted user information is appended to a file for later use. The main concern right now is that when I have to read a specific user's information later on, I won't be able to read a specific line if they end up being different lengths, so I was wondering if anyone has suggestions on how I can approach this project with those sorts of functionalities in mind.
Any insight would be greatly appreciated =)
Edit: I'm reading the files with ifstream. That's the only way I know how anyways.
Upvotes: 0
Views: 1235
Reputation: 1878
You almost spotted the problem already. As there is no definite answer to your problem, consider my hints as a starting point:
\n
, you can of course read a specific line of your file. But you have to seek through the whole file from its beginning to locate that line by counting all the lines you skip.seek()
). You'd then probably need a means of marking the length of your lines because you are going to extend shorter lines to the common length.Upvotes: 0