简体   繁体   中英

C++ reading specific parts of a text file

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.

You almost spotted the problem already. As there is no definite answer to your problem, consider my hints as a starting point:

  • Given that the lines in your file are separatet, eg by a linefeed \\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.
  • If all the lines had the same common length, you could compute the position in the file where a specific line starts and then read it (cf. 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.
  • As a compromise, in case you suffer wasting costly space by extending many short lines to a rather long common length, you could introduce a second index file. In that file, you just store, eg starting positions (numbers) that point into the actual data file. The indices (starting positions) can all be the same length (they're short compared to the data itself) so you can easily locate a specific one in the index . Then use that to look up the actual data.
  • You could use some sort of structure for your files. There are plenty, like XML or JSON. Most of them, however, also browse the file to locate the bits of information.
  • There are many in-line database libraries, such as SQLite or Berkeley DB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM