首页 \ 问答 \ C ++返回字符串不断变得垃圾(C++ return string keeps getting junk)

C ++返回字符串不断变得垃圾(C++ return string keeps getting junk)

为什么这里的返回字符串上有各种各样的垃圾?

string getChunk(ifstream &in){
char buffer[5];
for(int x = 0; x < 5; x++){
    buffer[x] = in.get();
    cout << x << " " << buffer[x] << endl;
}
cout << buffer << endl;
return buffer;
}

ifstream openFile;
openFile.open ("Bacon.txt");
chunk = getChunk(openFile);
cout << chunk;

虽然我的调试说我的缓冲区正在填充正确的字符,但我在字符串中得到了一堆垃圾,它的末尾有垃圾。

谢谢,c ++比Java困难得多。


Why does the return string here have all sorts of junk on it?

string getChunk(ifstream &in){
char buffer[5];
for(int x = 0; x < 5; x++){
    buffer[x] = in.get();
    cout << x << " " << buffer[x] << endl;
}
cout << buffer << endl;
return buffer;
}

ifstream openFile;
openFile.open ("Bacon.txt");
chunk = getChunk(openFile);
cout << chunk;

I get a load of junk in the string where it has junk on the end of it, even though my debug says that my buffer is being filled with the correct characters.

Thanks, c++ is a lot harder than Java.


原文:https://stackoverflow.com/questions/15890891
更新时间:2022-11-08 08:11

最满意答案

欢迎来到无国籍编程世界; 呈现页面后,变量无法生存; 您需要使用Session对象来保存它们。

在按钮单击中,设置Session(“PersonName”)=“Dave”

在Page_Load中,有以下代码:

str = Session(“PersonName”)。ToString()

我强烈建议您通过ASP.NET编程中的一些教程来熟悉您需要的所有概念。


Welcome to the world of stateless programming; variables don't survive after a page is rendered; you need to use the Session object to save them.

In your button click, set Session("PersonName") = "Dave"

In Page_Load, have code such as:

str = Session("PersonName").ToString()

I would highly recommend working through a few tutorials in ASP.NET programming to familarize yourself with all the concepts you need.

相关问答

更多
  • 欢迎来到无国籍编程世界; 呈现页面后,变量无法生存; 您需要使用Session对象来保存它们。 在按钮单击中,设置Session(“PersonName”)=“Dave” 在Page_Load中,有以下代码: str = Session(“PersonName”)。ToString() 我强烈建议您通过ASP.NET编程中的一些教程来熟悉您需要的所有概念。 Welcome to the world of stateless programming; variables don't survive after ...
  • 正如Marc Gravell已经提到的那样,您可以使用lambda表达式。 这是它在VB中的工作原理: Private Sub Calculate(f As Func(Of Double, Double, Double)) Dim a As Double = 1.5, b As Double = 3.14 Console.WriteLine(f(a,b)); End Sub 然后你可以这样调用Calculate: Calculate(Function(x,y) x+y) Calculate ...
  • 然后改变这个 If fl IsNot file(0) Then 'THIS_LINE 至 If Not fl.Equals(file(0)) Then 'THIS_LINE 边注: 您已经两次声明了fl ,所以如果不是这样,请更改它 Dim fl As String = "asd"` 对此 fl = "asd" Then change this If fl IsNot file(0) Then 'THIS_LINE to If Not fl.Equals(file(0)) Then 'THIS_L ...
  • 我会做Dim x As New Exception("this is a test") 。 最好的两个世界,没有推断,但你仍然只需要输入一次Exception :) I'd do Dim x As New Exception("this is a test"). Best of both worlds, no infering but you still only have to type Exception once :)
  • 抱歉,没有。 另一方面,它会变得非常混乱,因为VB.NET对赋值和相等使用相同的运算符。 If a = b Then 'wait, is this an assignment or comparison?! 相反,只需设置变量并进行比较: Dim customer As Customer = Nothing customer = GetCustomer(id) If IsNothing(customer) Then Return False End If Sorry, no. On the ...
  • 查看其他SO答案的答案 所以这应该工作: With New SomeObjectType("abc", 10) End With See the answers to this other SO Answer So this should work: With New SomeObjectType("abc", 10) End With
  • COLORREF基本上是32位整数值。 您可以像这样定义一个与它交互的结构(有关详细信息,请参阅此链接 ): Structure COLORREF Public R As Byte Public G As Byte Public B As Byte Public Overrides Function ToString() As String Return String.Format("({0},{1},{2})", R, G, B) End Function End Stru ...
  • VB.NET中的直接等价物称为反射 。 Reflection允许您在运行时检查类型及其成员的名称。 但是,它通常只应作为最后的手段使用。 通常在切换语言时,直接等效不是最佳选择,因为范例是不同的。 即使您确实使用了反射,它也不适用于局部变量,因此您可能需要大量重新设计代码。 更好的解决方案可能是使用Dictionary对象。 .NET中的Dictionary类是哈希表的一种实现,它允许您轻松存储键/值对。 因此,不是将值存储在变量中,而是可以将其存储在Dictionary ,如下所示: Dim d As N ...
  • VB.Net中没有固有的块范围语法,最常见的方式是您在问题中给出的第一个示例。 I found this answer on a Japanese site. With Nothing Dim A = ... Dim B = ... (Code using A & B) End With from http://www.ilovex.co.jp/Division/ITD/archives/2007/11/with_nothing.html (Translated from Japanese) ...
  • 对的,这是可能的。 正如你所说的那样:使用全局变量。 有关SO的相关问题,请参阅以下链接: 在Visual Studio 2010和VB.NET中声明全局变量 如果该方法被声明为Public (=从课外可见),您可以从解决方案中的其他位置调用您的函数。 看看这里: http://www.homeandlearn.co.uk/NET/nets11p3.html Yes, it is possible. Just as you called it: With global variables. See the ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)