C++ 如何将txt文件编号分为正数txt文件和负数txt文件

C++ 如何将txt文件编号分为正数txt文件和负数txt文件,c++,C++,你有两个主要问题。一种是良性的,另一种是导致未定义行为的 良性的问题在于声明 -9 -8 -7 -6 -5 -4 -3 -2 -1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 这里您对pozitivi、negative和masivs使用相同的索引x。此索引仅对masivs有效 现在使用单个索引x的循环不仅会在pozitivi和negative数组中打洞,还会超出这两个数组的范围 解决方案是添加另一个索引,分别为pozitivi和negative数组添加一个索引

你有两个主要问题。一种是良性的,另一种是导致未定义行为的

良性的问题在于声明

-9
-8
-7
-6
-5
-4
-3
-2
-1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
这里您对
pozitivi
negative
masivs
使用相同的索引
x
。此索引仅对
masivs
有效

现在使用单个索引
x
的循环不仅会在
pozitivi
negative
数组中打洞,还会超出这两个数组的范围

解决方案是添加另一个索引,分别为
pozitivi
negative
数组添加一个索引:

for (x = 0; x < 24; x++)
    if (masivs[x] >= 0)
        pozitivi[x] = masivs[x];
    else
        negativi[x] = masivs[x];
无符号p=0;//正数数组的索引
无符号n=0;//负数数组的索引
用于(无符号x=0;x<24;x++)
{
如果(masivs[x]>=0)
pozitivi[p++]=masivs[x];
其他的
负i[n++]=masivs[x];
}
之后,您可以使用
p
n
作为相应数组的大小,以便在将结果写入文件时使用


正如在对问题的评论中所提到的,实际上并不需要数组,这使得大部分代码都没有意义,整个程序也简单得多。简单性更好,因为出错的可能性更小。

您可以执行一个简单的while(getline())循环

#包括
#包括
#包括
#包括
#包括
int main()
{
int lineNumber,它=0;
std::字符串行;
std::ifstream f(“f.txt”);
流f1,f2的标准::;
f1.打开(“f1.txt”);
f2.打开(“f2.txt”);
while(std::getline(f,line))//读取字符串中的每一行f.txt
{
尝试
{
线号=标准::stoi(线);
}
catch(std::invalid_argument&e)//如果值不是数字
{

std::是否不需要中间数组来解决此问题。您需要三个文件(源、正片和负片),并且能够从源代码中一次读取一个值,并根据该值是否为负值将其填充到适当的输出文件中。所有数组工作、索引等都是毫无意义的,只是bug的滋生地(其中有很多)与@WhozCraig所说的一样,你最好将数字与文件输入/输出任务分开。然后,每个任务都可以单独测试。将复杂的任务分解为简单的部分并单独测试,是开发更大程序的关键。为什么不
getline
?为什么不
f>>value
,如果文件中有非数字值,程序应该退出。更不用说你没有真正验证字符串是否真的是一个数字,并且会将非数字数据写为负数。所讨论的文件只包含数字。如果他担心这些值是非数字的,他可以添加一个检查。我假设添加更多head只会让问题变得比需要谢谢你的更复杂!我是一个大菜鸟,所以这真的很有帮助!@Ray8r这里的人很无情,一定要做你的研究,并在发布到这里之前考虑你的问题。这将帮助你成为一个更好的问题解决者:)注意,因为
n
p
都是d对于for循环来说,除了控制结构之外,它们的有用性没有任何终极价值。之后,由于
n
p
都消失了,您不知道读取了多少负值和正值。假设我们保留这些不必要的数组,
n
p
应该移出(和之前)此循环用于除此之外的访问-loop@WhozCraig是的,更新了。谢谢
f2<<negativi<<endl;
f2 << &negativi[0] << endl;
for (x = 0; x < 24; x++)
    if (masivs[x] >= 0)
        pozitivi[x] = masivs[x];
    else
        negativi[x] = masivs[x];
unsigned p = 0;  // Index for positive number array
unsigned n = 0;  // Index for negative number array

for (unsigned x = 0; x < 24; x++)
{
    if (masivs[x] >= 0)
        pozitivi[p++] = masivs[x];
    else
        negativi[n++] = masivs[x];
}
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <fstream>

int main()
{
  int lineNumber, it = 0;
  std::string line;
  std::ifstream f("f.txt");
  std::ofstream f1, f2;
  f1.open("f1.txt");
  f2.open("f2.txt");
  while(std::getline(f, line)) //reads each line of f.txt ino a string
  {
    try
    {
      lineNumber = std::stoi(line);
    }
    catch(std::invalid_argument& e) //if value is not a number
    {
      std::cout << "VALUE IS NOT A NUMBER ON ITERATION " << it << '\n';
      //std::abort();
      continue;
    }
    catch(std::out_of_range& e) //if value is above +-2^32 +-1
    {
      std::cout << "VALUE OUT OF RANGE ON ITERATION " << it << '\n';
      //std::abort();
      continue;
    }
    if(lineNumber > 0)
    {
      f1 << lineNumber << '\n';
    }
    else
    {
      f2 << lineNumber << '\n';
    }
    it++;
  }
  f1.close();
  f2.close();
}