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
最满意答案
欢迎来到无国籍编程世界; 呈现页面后,变量无法生存; 您需要使用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 ...
-
使用变量作为符号vb.net(Using Variable as symbols vb.net)[2024-01-06]
正如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 ... -
VB.NET变量检查(VB.NET variable check)[2022-05-04]
然后改变这个 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 ... -
VB.Net变量声明(VB.Net variable declaration)[2023-03-17]
我会做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
-
VB.net API变量(VB.net API variables)[2024-02-13]
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人工范围(vb.net artificial scope)[2024-04-12]
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 ...